﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FarsiLibrary.Win;
using System.IO.Ports;
using CIRDiagnoseDevice.DBManage;
using CIRDiagnoseDevice.Test;
using System.Threading;
using System.Collections;
using System.Runtime.InteropServices;
using System.IO;

namespace CIRDiagnoseDevice
{
    public delegate void SetToolStripStatusInfo(string str);
    public partial class MainFrm : Form
    {

        #   region 全局变量
        Statistics statistic = new Statistics();
        public static DeviceInfo device = new DeviceInfo();
        public static MainFrm MotherFrm;
        public SerialPort OSP5;  //与接口单元通信串口
        private SerialPort PowerPort; // 与电源单元通信串口
        public bool StopTest; // 停止测试标志
        public bool Suspend; // 暂停测试标志 
        public string Tester; // 保存测试员
        public string Dev_num; // 保存CIR编号
        public string TestTime; // 保存测试时间
        public bool TestTran;  // 测试发射机指标标志
        public bool TransTestSaved = true;// 450M发射机测试结果是否保存标志
        public bool TestCoRec; // 测试同频接收机指标标志
        public bool CoRecTestSaved = true; // 450M同频接收机测试结果是否保存标志
        public bool TestPiRec; // 测试异频接收机指标标志
        public bool PiRecTestSaved = true; // 450M异频接收机测试结果是不保存标志
        public bool TestSig;  // 测试信令指标标志
        public bool SigTestSaved = true; //  信令测试结果是否保存标志
        public bool TestGSM_RVo; // 测试GSM-R语音模块标志
        public bool GSM_RVoTestSaved = true; // GSM-R语音测试结果是否保存标志
        public bool TestGSM_RDig; // 测试GSM-R数据模块标志
        public bool GSM_RDigTestSaved = true; // GSM-R数据测试结果是否保存标志
        public bool TestLBJFun; // LBJ功能试验标志
        public bool LBJFunTestSaved = true; // LBJ功能试验结果是否保存标志
        public bool TestTrainEnd; // 450M列尾功能试验标志
        public bool TrainEndTrialSaved = true; // 450M列尾功能试验是否保存标志
        public bool DeviceInfoSaved;// 标志已经测试过的设备信息是否保存标志
        public bool GSM_RFunTrial; // 标志GSM-R数据功能试验
        public bool GSM_RFunTrialSaved = true; // GSM-R功能试验是否保存标志
        public bool TrainApproachEarly_Warning; // 标志列车接近预警功能试验
        public bool TrainApproachTrialSaved = true; // 列车接近预警功能试验是否保存标志
        public bool _450MDispatchFunTrial; // 标志450M调度命令功能试验
        public bool _450MDispatchTrialSaved = true; // 450M调度命令功能试验是否保存标志
        public bool Test450MQuota;//整机测试450M指标
        public bool _450MQuotaTestSaved = true; // 450M指标测试是否保存标志
        public bool HeluqiTestSave = true; // 合路器测试是否保存
        public bool TestGsmQuota; // 整机测试GSM指标
        public bool GsmQuotaTestSaved = true; // GSM指标测试是否保存标志
        public bool TrialWholeFun;//整机功能试验
        public bool WholeFunTrialSaved = true; // 整机功能试验是否保存标志
        public bool LbjQuotaTestSaved = true; // 800M指标测试是否保存标志
        public bool CallSuccessed; // 标志呼叫是否成功
        public bool TestLbjTran; // LBJ发射机测试标志
        public bool LbjTranTestSaved = true; // Lbj发射机指标测试是否保存标志
        public bool TestLbjRec; // LBJ接收机测试标志
        public bool LbjRecTestSaved = true; // Lbj接收机指标测试是否保存标志
        public Thread Test450MDispatchThread; // 450M调度命令试验线程
        public Thread Test450M_TranThread; // 450M发射测试线程
        public Thread Test450M_CoRecThread; // 450M同频接收机测试线程
        public Thread Test450M_PiRecThread; // 450M异频接收机测试线程
        public Thread Test450M_SigThread;   // 450M信令测试线程
        public Thread TestGSM_RDigThread; // GSM-R数据模块测试线程
        public Thread TestGSM_RVoithread; //GSM-R数据模块测试线程
        public Thread TestLbjThread; // Lbj试验线程
        public Thread TestLbjTranThread; // Lbj发射机模块测试线程
        public Thread TestLbjRecThread; //Lbj接收机模块测试线程
        public Thread DealATCommThread; // 处理AT命令线程
        public Thread Test450MTrainEndThread; // 450M列尾功能试验线程
        public Thread TrainApproachTrialThread; //列车接近预警试验线程
        public Thread GSMFunTrialThread;     // GSM-R数据模块功能试验线程
        public Thread TestHeLuQiThread;//合路器测试线程。
        public bool IsTestTotal;   // 标志是否整机测试
        public bool TestTotalSaved = true; // 整机指标测试结果是否保存
        public bool IsGsmModTest; // 标志是否是Gsm单板测试
        private bool TestTotalQuota;//整机指标测试标志
        private SingleModFrm SingleModform;// 单项测试窗体对象 
        private SingleModelTestFrm frm;
        private string preDev_num; // 前一CIR编号
        private string curDev_num; // 当前CIR编号
        private bool IsExportPDF;  // 是否将测试结果导出到PDF文件标志
        private List<ExportDataStruct> TotalTestData; // 保存同一设备的所有测试项目
        private bool IsSaveAll;    // 标志是否所有测试结果
        private PrintRecord printRecord; //打印测试结果
        public string formName; // 保存单项测试时测试项的窗体名
        public SerialPort _2944SerialPort; // 与2944仪表通信串口
        public SerialPort _4202SerialPort; //与4202仪表通信串口
        public SerialPort TrainEndSerialPort; // 与450M列尾单元通信串口
        public SerialPort LbjSerialPort;        // 与Lbj模拟单元通信串口
        public SerialPort GsmFunSerialPort; //与Gsm功能试验单元通信串口
        public SerialPort TaxSerialPort; // 发送Tax数据串口
        public SerialPort GSM_RModPort;// GSM-R模块单独测试通信串口
        public SerialPort PocsagCodePort;//列车接近预警功能试验通信串口
        public SerialPort LbjNorSerialPort; //与Lbj通信串口
        public SerialPort LbjControlPort; // Lbj控制通信串口
        public float TestFreSetValue;   // 测试频点选择
        public float TestPiFreSetValue; // 异频测试频点值
        private int PowerValue;// 测试功率值
        private byte[] RBuffer = new byte[500]; // 接收测试仪返回结果
        private byte[] RXBuffer = new byte[2048];//缓冲接收数据
        //private List<FATabStripItem> TabItemList = new List<FATabStripItem>();
        SerialPort OSP = new SerialPort();
        TestRecordManage manage = new TestRecordManage();
        System.Timers.Timer DisplayTime = new System.Timers.Timer(); //实时刷新显示系统时间
        WholeDeviceTestFrm form;
        private CommunicationWithInterfaceUnit unit;
        public Queue queue;
        public Queue queue2;
        public int Index;
        public byte CabNum1, CabNum2, CabNum3, CabNum4;
        public bool Test110Power, Test24vPower;
        public float sensThreshold; // 发射机调制灵敏度标准值设置
        private string manufactureName; // CIR设备生产商名称
        public string LbjManufName;  //Lbj设备生产商名称
        private LbjTestCommand lbjCommand = new LbjTestCommand();
        private string StripInfo; // 停止或暂停测试时状态栏所提示的信息
        #  endregion

        /// <summary>
        /// 前一测试CIR设备编号
        /// </summary>
        public string PreDev_num
        {
            set { preDev_num = value; }
            get { return preDev_num; }
        }

        /// <summary>
        /// 当前测试CIR设备编号
        /// </summary>
        public string CurDev_num
        {
            set { curDev_num = value; }
            get { return curDev_num; }
        }

        public string ManufactureName
        {
            set { manufactureName = value; }
            get { return manufactureName; }
        }

        private struct ExportDataStruct
        {
            public List<DataTable> tableList;
            public List<string> tableNameList;
        }  ExportDataStruct exportDataStruct;

        public float GetFreValue
        {
            get { return TestFreSetValue; }
        }

        public float GetPiFreValue
        {
            get { return TestPiFreSetValue; }
        }

        public MainFrm()
        {
            InitializeComponent();
            TestFreSetValue = 457.7f;
            TestPiFreSetValue = TestFreSetValue + 10.0f;
            MotherFrm = this;
            ReadiniFile initFile = new ReadiniFile(Application.StartupPath);
            List<string> list = new List<string>();
            list = initFile.ReadCommPort();
            string portName = string.Empty;
            int baudeRate = 0;
            string[] strArr = list[0].Split(new Char[] { ',' });
            portName = strArr[0].Trim();
            baudeRate = int.Parse(strArr[1].Trim());
            _2944SerialPort = new SerialPort(portName, baudeRate);

            strArr = list[1].Split(new Char[] { ',' });
            portName = strArr[0].Trim();
            baudeRate = int.Parse(strArr[1].Trim());
            _4202SerialPort = new SerialPort(portName, baudeRate);

            strArr = list[2].Split(new Char[] { ',' });
            portName = strArr[0].Trim();
            baudeRate = int.Parse(strArr[1].Trim());
            TrainEndSerialPort = new SerialPort(portName, baudeRate);

            strArr = list[3].Split(new Char[] { ',' });
            portName = strArr[0].Trim();
            baudeRate = int.Parse(strArr[1].Trim());
            LbjSerialPort = new SerialPort(portName, baudeRate);

            strArr = list[4].Split(new Char[] { ',' });
            portName = strArr[0].Trim();
            baudeRate = int.Parse(strArr[1].Trim());
            GsmFunSerialPort = new SerialPort(portName, baudeRate);

            strArr = list[5].Split(new Char[] { ',' });
            portName = strArr[0].Trim();
            baudeRate = int.Parse(strArr[1].Trim());
            TaxSerialPort = new SerialPort(portName, baudeRate);

            strArr = list[6].Split(new Char[] { ',' });
            portName = strArr[0].Trim();
            baudeRate = int.Parse(strArr[1].Trim());
            GSM_RModPort = new SerialPort(portName, baudeRate);

            strArr = list[7].Split(new Char[] { ',' });
            portName = strArr[0].Trim();
            baudeRate = int.Parse(strArr[1].Trim());
            PocsagCodePort = new SerialPort(portName, baudeRate);

            strArr = list[8].Split(new Char[] { ',' });
            portName = strArr[0].Trim();
            baudeRate = int.Parse(strArr[1].Trim());
            PowerPort = new SerialPort(portName, baudeRate);

            strArr = list[9].Split(new Char[] { ',' });
            portName = strArr[0].Trim();
            baudeRate = int.Parse(strArr[1].Trim());
            OSP5 = new SerialPort(portName, baudeRate);

            strArr = list[10].Split(new Char[] { ',' });
            portName = strArr[0].Trim();
            baudeRate = int.Parse(strArr[1].Trim());
            LbjNorSerialPort = new SerialPort(portName, baudeRate);

            strArr = list[11].Split(new Char[] { ',' });
            portName = strArr[0].Trim();
            baudeRate = int.Parse(strArr[1].Trim());
            LbjControlPort = new SerialPort(portName, baudeRate);

            OpenSerialPort(LbjNorSerialPort);
            OpenSerialPort(OSP5);
            OpenSerialPort(PowerPort);
            GSM_RModPort.ReadTimeout = 30000;
            queue = new Queue();
            queue2 = new Queue();
            PowerPort.DataReceived += new SerialDataReceivedEventHandler(PowerPort_DataReceived);
            GSM_RModPort.DataReceived += new SerialDataReceivedEventHandler(GSM_RModPort_DataReceived);
            OpenSerialPort(GSM_RModPort);
            unit = new CommunicationWithInterfaceUnit();
            DealATCommThread = new Thread(new ThreadStart(DealWithATData));
            DealATCommThread.IsBackground = true;
            深圳长龙.PerformClick();
            深圳长龙L.PerformClick();
            //DealATCommThread.Start();
        }

        public ulong HexToBCD(ulong phex, uint num)//HEX码转换成BCD码
        {
            int i;
            ulong pdiv, pmul;

            byte[] tmp = new byte[num];
            pdiv = phex;
            for (i = 0; i < num; i++)
            {
                tmp[i] = (byte)(pdiv % 10);
                pdiv = pdiv / 10;
            }
            pmul = 1;
            pdiv = 0;
            for (i = 0; i < num; i++)
            {
                pdiv = pdiv + pmul * tmp[i];
                pmul = pmul * 0x10;
            }
            return pdiv;
        }

        public void StartThread()
        {
            OpenSerialPort(GSM_RModPort);
            DealATCommThread = new Thread(new ThreadStart(DealWithATData));
            DealATCommThread.IsBackground = true;
            DealATCommThread.Start();
        }

        public void StopThread()
        {
            if (DealATCommThread != null)
            {
                try
                {
                    DealATCommThread.Abort();
                }
                catch
                {

                }
            }
        }

        public void DealWithATData()
        {
            List<byte> readList = new List<byte>();
            string commStr = string.Empty;
            while (!StopTest)
            {
                if (queue2.Count != 0)
                {
                    try
                    {
                        readList.Add((Byte)queue2.Dequeue());

                        if (readList.Contains((byte)0x0A))
                        {
                            string[] strArr;
                            byte[] readByte = new byte[readList.Count];
                            readByte = readList.ToArray();
                            string readStr = System.Text.ASCIIEncoding.Default.GetString(readByte, 0, readByte.Length);
                            readStr = readStr.Trim(new Char[] { '\r', '\n' });
                            readStr = readStr.Trim(new Char[] { '\\' });
                            strArr = readStr.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                            readList.Clear();
                            foreach (string s in strArr)
                            {
                                if (!SingleModform.GsmVoTest.haveReadIMSI)
                                {
                                    if ((s == "+CUUS1U: 1,\"00\"" || s == "+CREG: 1,\"0072\",\"1000\"" || s == "+CRING: VOICE") && SingleModform.GsmVoTest.haveCall)  //s == "+CLIP: \"\",129" ||
                                    {
                                        commStr = "ATA\r\n";
                                        byte[] sendbyte = System.Text.ASCIIEncoding.Default.GetBytes(commStr);
                                        GSM_RModPort.Write(sendbyte, 0, sendbyte.Length);
                                    }
                                }
                            }
                        }
                    }
                    catch
                    {

                    }
                }
            }
        }

        void GSM_RModPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            while (!MainFrm.MotherFrm.StopTest)
            {
                while (!MainFrm.MotherFrm.Suspend)
                {
                    if (MainFrm.MotherFrm.IsGsmModTest)
                    {
                        string str2 = string.Empty;
                        string str = string.Empty;
                        string commStr = string.Empty;
                        int Num = 0;
                        try
                        {
                            byte readByte = (byte)GSM_RModPort.ReadByte();
                            lock (queue2)
                            {
                                queue2.Enqueue(readByte);
                            }

                        }
                        catch (System.Exception ex)
                        {
                            if (ex.GetType() == typeof(TimeoutException))
                            {
                                if (SingleModform.GsmVoTest != null)
                                {
                                    if (SingleModform.GsmVoTest.haveCall)
                                    {
                                        //commStr = "ATA\r\n";
                                        //byte[] sendbyte = System.Text.ASCIIEncoding.Default.GetBytes(commStr);
                                        //GSM_RModPort.Write(sendbyte, 0, sendbyte.Length);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        void PowerPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte data = (byte)PowerPort.ReadByte();
            if (Test110Power)
            {
                MessageBox.Show("110V电源功率为：" + data.ToString() + "W");
                Test110Power = false;
            }
            if (Test24vPower)
            {
                MessageBox.Show("24V电源功率为：" + data.ToString() + "W");
                Test24vPower = false;
            }
        }

        public void MainFrm_OnButtonClick(object sender)
        {
            textEdit4.Text = device.Dev_num;
            RunOrStop.Enabled = true;
            curDev_num = device.Dev_num;
            Dev_num = curDev_num;
            ChangeButtonText();
        }

        private void InitParameter()
        {
            StopTest = true;
            Suspend = true;
            TestCoRec = false;
            TestTrainEnd = false;
            TestTran = false;
            TestPiRec = false;
            TestLBJFun = false;
            TestGSM_RVo = false;
            TestGSM_RDig = false;
            TestSig = false;
            GSM_RFunTrial = false;
            TrainApproachEarly_Warning = false;
            _450MDispatchFunTrial = false;
            GSM_RDigTestSaved = true;
            GSM_RFunTrialSaved = true;
            GSM_RVoTestSaved = true;
            LBJFunTestSaved = true;
            SigTestSaved = true;
            TrainEndTrialSaved = true;
            TransTestSaved = true;
            CoRecTestSaved = true;
            PiRecTestSaved = true;
            TrainApproachTrialSaved = true;
            _450MDispatchTrialSaved = true;
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            try
            {
                DevExpress.Accessibility.AccLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressUtilsLocalizationCHS();
                DevExpress.XtraBars.Localization.BarLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraBarsLocalizationCHS();
                DevExpress.XtraCharts.Localization.ChartLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraChartsLocalizationCHS();
                DevExpress.XtraEditors.Controls.Localizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraEditorsLocalizationCHS();
                DevExpress.XtraGrid.Localization.GridLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraGridLocalizationCHS();
                DevExpress.XtraLayout.Localization.LayoutLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraLayoutLocalizationCHS();
                DevExpress.XtraPrinting.Localization.PreviewLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraPrintingLocalizationCHS();
                DevExpress.XtraSpellChecker.Localization.SpellCheckerLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraSpellCheckerLocalizationCHS();
                DevExpress.XtraTreeList.Localization.TreeListLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraTreeListLocalizationCHS();

                DisplayTime.Elapsed += new System.Timers.ElapsedEventHandler(DisplayTime_Elapsed);
                DisplayTime.Interval = 10000;
                DisplayTime.Enabled = true;
                SetToolStripLabel(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
                exportDataStruct.tableNameList = new List<string>();
                exportDataStruct.tableList = new List<DataTable>();
                // 检查是否存在数据库与数据表如果不存在就创建它们
                try
                {
                    string dbName = manage.CreateDB("testResult");
                    string sql = " IF NOT EXISTS( SELECT 1 FROM SYSOBJECTS WHERE ID = OBJECT_ID('test_rec') AND TYPE ='U')" +
                                      "CREATE TABLE test_rec (" +
                                      "设备编号   varchar(50)   NOT NULL," +
                                      "测试时间  datetime  NOT NULL," +
                                      "测试项目  varchar(MAX)   NOT NULL," +
                                      "单位        varchar(50)," +
                                      "指标要求   varchar(MAX) ," +
                                      "测试值     varchar(MAX) ," +
                                      "测试结果   varchar(MAX)   NOT NULL," +
                                      "测试员      varchar(50)  NOT NULL," +
                                      "模块名称   varchar(MAX)   NOT NULL);";
                    string sql2 = "IF NOT EXISTS(SELECT 1 FROM SYSOBJECTS WHERE ID = OBJECT_ID('deviceInfo') AND TYPE ='U')" +
                                        "CREATE TABLE deviceInfo (" +
                                          "机车属局  nvarchar(50) , " +
                                           "机车属段  nvarchar(50) , " +
                                            "机车属所  nvarchar(50) , " +
                                            "机车编号  nvarchar(50) , " +
                                             "机车端别  int , " +
                                              "机车修程  nvarchar(50) , " +
                                              "CIR属局   nvarchar(50) ," +
                                              "CIR属段   nvarchar(50) ," +
                                              "CIR属车间 nvarchar(50) ," +
                                              "CIR属工区 nvarchar(50) ," +
                                              "CIR生产商 nvarchar(50) ," +
                                               "CIR型号   nvarchar(50) ," +
                                               "CIR编号 nvarchar(50) PRIMARY KEY  NOT NULL," +
                                               "CIR修程   nvarchar(50));";
                    StringBuilder sb = new StringBuilder();
                    sb.Append("IF NOT EXISTS(SELECT * FROM dbo.sysobjects WHERE name='lbjInfo' AND TYPE = 'U')");
                    sb.Append("CREATE TABLE lbjInfo (");
                    sb.Append("LBJ型号 nvarchar(50),");
                    sb.Append("LBJ编号 nvarchar(50),");
                    sb.Append("LBJ生产商 nvarchar(50)); IF NOT EXISTS(SELECT * FROM dbo.sysobjects WHERE name='tb_TestIndexConfig' AND TYPE = 'U')");
                    sb.Append("CREATE TABLE tb_TestIndexConfig (");
                    sb.Append(" [TestGroupID] [int] NOT NULL CONSTRAINT [DF_tb_TestIndexConfig_TestGroupID]  DEFAULT ((0)),");
                    sb.Append(" [TestID] [int] NOT NULL CONSTRAINT [DF_tb_TestIndexConfig_TestID]  DEFAULT ((0)),");
                    sb.Append(" TestGroup nvarchar(50) COLLATE Chinese_PRC_CI_AS NOT NULL,");
                    sb.Append(" TestName nvarchar(50) COLLATE Chinese_PRC_CI_AS NOT NULL,");
                    sb.Append(" unit varchar(10) COLLATE Chinese_PRC_CI_AS NULL,");
                    sb.Append("minimum float,");
                    sb.Append("maximum float,");
                    sb.Append("exponent int  NOT NULL CONSTRAINT [DF_tb_TestIndexConfig_exponent]  DEFAULT ((0)),");
                    sb.Append(" remark nvarchar(200)  COLLATE Chinese_PRC_CI_AS NULL, ");
                    sb.Append("displaytype int Not Null  DEFAULT ((1)),");
                    sb.Append("defaultConfig int Not Null  DEFAULT ((1)),");
                    sb.Append("userminimum float,");//用户的配置的最低标准
                    sb.Append("usermaximum float,");//用户的配置的最高标准
                    sb.Append("InsertTime datetime   NOT NULL CONSTRAINT [DF_tb_TestIndexConfig_today]  DEFAULT (getdate()),");
                    sb.Append("  CONSTRAINT [PK_tb_TestIndexConfig] PRIMARY KEY CLUSTERED ");
                    sb.Append(" ( [TestGroup] ASC,[TestName] ASC)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] ");
                    sb.Append(" EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'每一个TestGroup内的TestName的测试顺序' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tb_TestIndexConfig', @level2type=N'COLUMN',@level2name=N'seq'");
                    sb.Append("  EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'1代表默认指标，不会删掉一直保留。0代表用户指标' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tb_TestIndexConfig', @level2type=N'COLUMN',@level2name=N'defaultConfig' ");
                    sb.Append(" EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'测试表格展现形式' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tb_TestIndexConfig', @level2type=N'COLUMN',@level2name=N'displaytype' ");
                    //sb.Append(" EXECUTE sp_addextendedproperty N'MS_Description', @v, N'SCHEMA', N'dbo', N'TABLE', N'Tmp_tb_TestIndexConfig', N'COLUMN', N'defaultConfig' go");

                    manage.CreateTable(dbName, sb.ToString());
                    manage.CreateTable(dbName, sql);
                    manage.CreateTable(dbName, sql2);

                    manage.OpenDataBase();
                    #region 导入初始配置文件
                    #region 导入测试项目信息及指标文件
                    if (manage.ExecuteSqlScalar("select top 1 * from tb_TestIndexConfig") == null)
                    {
                        string filePath = System.Windows.Forms.Application.StartupPath;//System.Environment.CurrentDirectory;
                        string pathto = filePath + @"\" + "TestDetailConfig.xls";
                        if (!InitConfigExcel(pathto, 1))
                        {
                            MessageBox.Show("初始化测试项目及指标失败，请联系管理员！");
                            return;
                        }

                    }
                    #endregion
                    #endregion


                }
                catch
                {
                    MessageBox.Show("初始化测试项目及指标失败，请联系管理员！");
                }
                manage.OpenDataBase();
                TestTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 初始化excel类型的配置文件（如：测试项目及标准）
        /// </summary>
        /// filename文件路径
        /// type配置类型：1.测试项目及标准
        private bool InitConfigExcel(string filename, byte type)
        {
            manage.OpenDataBase();
            bool sucess = false;
            try
            {
                switch (type)
                {
                    case 1:
                        {
                            EXCELtoSQL ets = new EXCELtoSQL(filename);
                            DataTable dt = ets.xsldataTestConfig();
                            // DataTable dt = ets.xsldata();
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                if (dt.Rows[i]["科学计数单位"].ToString().Trim() == "")
                                {
                                    dt.Rows[i]["科学计数单位"] = 0;
                                }
                            }
                            if (manage.RestoreData(dt, "tb_TestIndexConfig"))
                            {
                                sucess = true;
                            }

                            break;
                        }
                    default:
                        break;
                }
            }
            catch (Exception)
            {


            }

            manage.CloseDataBase();
            return sucess;
        }
        #  region 关闭串口
        public void CloseSerialPort(SerialPort OSP)
        {
            if (OSP.IsOpen)
            {
                try
                {
                    OSP.Close();
                }
                catch (Exception e)
                {

                }
            }
        }
        #   endregion


        void DisplayTime_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            SetToolStripLabel(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
            TestTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
        }

        private void TotalTestMenuItem_Click(object sender, EventArgs e)
        {
            if (TransTestSaved && TrainEndTrialSaved && CoRecTestSaved && PiRecTestSaved &&
                       SigTestSaved && GSM_RDigTestSaved && GSM_RFunTrialSaved && GSM_RVoTestSaved &&
                       LBJFunTestSaved && _450MDispatchTrialSaved && TrainApproachTrialSaved)
            {
                int num = faTabStrip1.Items.Count;
                panelControl1.Visible = true;
                IsTestTotal = true;
                if (frm != null)
                {
                    for (int i = 0; i < num; i++)
                    {
                        float height = faTabStrip1.Items[faTabStrip1.Items.Count - 1].StripRect.Height;
                        int startX = (int)faTabStrip1.Items[faTabStrip1.Items.Count - 1].StripRect.X;
                        FATabStripItem item1 = faTabStrip1.GetTabItemByPoint(new Point(startX, (int)height));
                        if (item1 != null)
                            faTabStrip1.RemoveTab(item1);
                    }
                    frm.Close();
                    frm.Dispose();
                    frm = null;
                    textEdit4.Text = "";
                    textEdit3.Text = "";
                    curDev_num = "";
                    preDev_num = "";
                    SetStripButtonEnable(false);
                }
                if (!faTabStrip1.Visible)
                {
                    faTabStrip1.Show();
                    if (form == null)
                    {
                        FATabStripItem item = new FATabStripItem("整机测试", null);
                        faTabStrip1.AddTab(item);
                        form = new WholeDeviceTestFrm(ref TestGSM_RDigThread, ref TestGSM_RVoithread);
                        form.TopLevel = false;
                        form.FormBorderStyle = FormBorderStyle.None;
                        form.Dock = DockStyle.Fill;
                        form.Parent = this.faTabStrip1.Items[0];
                        form.Show();
                    }
                    else
                    {
                        faTabStrip1.SelectedItem = faTabStrip1.Items[0];
                    }
                }
                else
                {
                    if (form == null)
                    {
                        FATabStripItem item = new FATabStripItem("整机测试", null);
                        faTabStrip1.AddTab(item);
                        form = new WholeDeviceTestFrm(ref TestGSM_RDigThread, ref TestGSM_RVoithread);
                        form.TopLevel = false;
                        form.FormBorderStyle = FormBorderStyle.None;
                        form.Dock = DockStyle.Fill;
                        form.Parent = this.faTabStrip1.Items[0];
                        form.Show();
                    }
                    else
                    {
                        faTabStrip1.SelectedItem = faTabStrip1.Items[0];
                    }
                }

                InitParameter();
            }
            else
            {
                MessageBox.Show("请先保存测试结果");
            }
        }

        private void SingleTestMenuItem_Click(object sender, EventArgs e)
        {
            if (TestTotalSaved)
            {
                panelControl1.Visible = true;
                IsTestTotal = false;
                if (faTabStrip1.Items.Count > 0)
                {

                    int num = faTabStrip1.Items.Count;
                    if (form != null)
                    {
                        form.Close();
                        form.Dispose();
                        form = null;
                        FATabStripItem item = faTabStrip1.GetTabItemByPoint(new Point(10, 17));
                        if (item != null)
                        {
                            faTabStrip1.RemoveTab(item);
                            item.Dispose();
                        }
                        textEdit4.Text = "";
                        textEdit3.Text = "";
                        curDev_num = "";
                        preDev_num = "";
                        SetStripButtonEnable(false);
                    }
                    else
                    {
                        faTabStrip1.SelectedItem = faTabStrip1.Items[0];
                    }
                }
                if (faTabStrip1.Items.Count == 0)
                {
                    FATabStripItem item = new FATabStripItem("单项测试", null);
                    faTabStrip1.AddTab(item);
                    frm = new SingleModelTestFrm();
                    frm.TopLevel = false;
                    frm.Parent = this.faTabStrip1.Items[0];
                    frm.Dock = DockStyle.Fill;
                    frm.FormBorderStyle = FormBorderStyle.None;
                    frm.Show();
                    //// frm.MdiParent = this;
                    InitParameter();
                }
                if (!faTabStrip1.Visible)
                    faTabStrip1.Show();
            }
            else
            {
                MessageBox.Show("请先保存测试结果 ！");
            }
        }

        private void ExecuteSingleModTest(string modName)
        {
            SingleModform = (SingleModFrm)faTabStrip1.SelectedItem.Controls["SingleModFrm"];
            SingleModform.SetToolStripStatusInfo = SetToolStripStatusInfo;
            if (modName == "发射机性能指标测试")
            {
                SingleModform.StartCIR_450M_TranModTest(ref Test450M_TranThread, _2944SerialPort, queue);
            }
            else if (modName == "合路器测试")
            {
                SingleModform.StartHeLuQiTest(ref TestHeLuQiThread, LbjNorSerialPort, GSM_RModPort, queue);
                ResetGSMf_RDigMod();
            }
            else if (modName == "同频接收机性能指标测试")
            {
                SingleModform.StartCIR_450M_CoRecModTest(ref Test450M_CoRecThread, _2944SerialPort, queue);
            }
            else if (modName == "异频接收机性能指标测试")
            {
                SingleModform.StartCIR_450M_PiRecModTest(ref Test450M_PiRecThread, _2944SerialPort, queue);
            }
            else if (modName == "信令指标测试")
            {
                SingleModform.StartCIR_450M_SigModTest(ref Test450M_SigThread, _2944SerialPort, queue);
            }
            else if (modName == "450MHz列尾功能试验")
            {
                SingleModform.Start450MFunTrial(TrainEndSerialPort, ref Test450MTrainEndThread);
            }
            else if (modName == "GSM-R语音模块电性能测试")
            {
                SingleModform.StartGsmVoTest(_4202SerialPort, ref TestGSM_RVoithread, queue, GSM_RModPort);
            }
            else if (modName == "GSM-R数据模块电性能测试")
            {
                SingleModform.StartGsmDigTest(ref TestGSM_RDigThread, _4202SerialPort, queue);
            }
            else if (modName == "GSM-R数据模块功能试验")
            {
                SingleModform.StartGsmFunTrial(GsmFunSerialPort, TaxSerialPort, ref GSMFunTrialThread);
            }
            else if (modName == "LBJ功能试验")
            {
                SingleModform.StartLbjFunTrial(LbjSerialPort, ref TestLbjThread);
                TestStatusLabel1.Text = SingleModform.ToolStrip;
            }
            else if (modName == "GSM-R语音模块单板电性能测试")
            {
                SingleModform.StartGsmVoModTest(_4202SerialPort, ref TestGSM_RVoithread, queue, GSM_RModPort);
                ResetGSMf_RDigMod();
            }
            else if (modName == "450MHz调度命令功能试验")
            {
                SingleModform.Start450MDispatchTrial(TrainEndSerialPort, ref Test450MDispatchThread);
            }
            else if (modName == "GSM-R数据模块单板电性能测试")
            {
                SingleModform.StartGsmDigModTest(ref TestGSM_RDigThread, _4202SerialPort, queue);
                ResetGSMf_RDigMod();
            }
            else if (modName == "列车接近预警功能试验")
            {
                SingleModform.SartTrainApproachFunTrial(PocsagCodePort, ref TrainApproachTrialThread);
            }
            else if (modName == "LBJ发射机电性能测试")
            {
                if (OSP5.IsOpen == false)
                {
                    OpenSerialPort(OSP5);
                }
                unit.ChangeVoiceToLBJ(OSP5);
                SingleModform.StartLbjTranTest(_2944SerialPort, LbjNorSerialPort, ref TestLbjTranThread, queue);

            }
            else if (modName == "LBJ接收机电性能测试")
            {
                if (OSP5.IsOpen == false)
                {
                    OpenSerialPort(OSP5);
                }
                unit.ChangeVoiceToLBJ(OSP5);
                SingleModform.StartLbjRecTest(_2944SerialPort, LbjNorSerialPort, ref TestLbjRecThread, queue);
            }
        }

        #    region 复位GSM-R数据模块
        /// <summary>
        /// 复位GSM-R数据模块
        /// </summary>
        public void ResetGSMf_RDigMod()
        {
            if (OSP5.IsOpen)
            {
                unit.SendResetPower(OSP5);
                unit.Delay(2500);
                unit.SendClearResetPower(OSP5);
                unit.Delay(1000);
                unit.SendResetPower(OSP5);
            }
        }

        #   endregion

        # region 复位GPRS模块

        public void ResetGPRS_Mod()
        {
            unit.SendGPRSPowerOff(OSP5);
            unit.Delay(1500);
            unit.SendGPRSPowerOn(OSP5);
            unit.Delay(1000);
            unit.SendOpenGPRSPower(OSP5);
            unit.Delay(1000);
            unit.SendClearGPRSPower(OSP5);
            unit.Delay(2500);
            unit.SendOpenGPRSPower(OSP5);

        }

        # endregion

        #  region 整机测试时测试GSM-R语音模块
        public void TestGsmQuotaMethod()
        {
            unit.SendInterUnitCallOff(OSP5);
            form.TestGsmQuota(ref TestGSM_RVoithread, ref TestGSM_RDigThread);
            IsExportPDF = false;
        }

        #  endregion

        # region 整机测试时测试800M电性能指标

        public void TestLbjQuotaMethod()
        {
            unit.ChangeVoiceToLBJ(OSP5);
            form.TestLbjQuota(ref TestLbjTranThread, ref TestLbjRecThread);
            IsExportPDF = false;
        }

        # endregion

        #   region  整机试验Lbj功能

        public void TrialLbjFun()
        {
            form.TrialFunction(ref TestLbjThread);
            IsExportPDF = false;
        }

        public void Start450MFunTrial()
        {
            form.Trial450MFun(ref Test450MTrainEndThread);
        }

        public void TrialTrainApproach()
        {

        }
        #    endregion

        #    region 整机试验时试验450M调度命令
        public void Trial450MDispatch()
        {
            form.Trial450MDispath(TrainEndSerialPort, ref Test450MDispatchThread);
        }

        #   endregion

        private void SetGrid(DataGridView grid)
        {

            for (int i = 0; i < grid.Rows.Count; i++)
            {
                grid.Rows[i].Cells["测试值"].Value = i + 1;
                grid.Rows[i].Cells["测试结果"].Value = "合格";
            }
        }

        private void RunOrStop_Click(object sender, EventArgs e)
        {
            try
            {
                if (RunOrStop.Text == "开始测试")
                {
                    if (textEdit3.Text.Trim() != "")
                    {
                        try
                        {
                            RunOrStop.Text = "停止测试";
                            RunOrStop.Image = CIRDiagnoseDevice.Properties.Resources.player_stop;
                            RunOrStop.ToolTipText = "停止测试";
                            PauseOrContinue.Enabled = true;
                            Save.Enabled = true;
                            SaveAll.Enabled = true;
                            Export.Enabled = true;
                            printer.Enabled = true;
                            StopTest = false;
                            Suspend = false;
                            if (faTabStrip1.Items[0].Title == "整机测试")
                            {
                                // form.ClearDataGrid();
                                if (form.SelectedPageName == "450M电性能指标测试")
                                {
                                    form.Test450MQuota(ref Test450M_TranThread, ref Test450M_CoRecThread, ref Test450M_PiRecThread, ref Test450M_SigThread);
                                    TestTotalQuota = true;
                                    IsExportPDF = false;
                                }
                                else if (form.SelectedPageName == "GSM-R电性能指标测试")
                                {
                                    unit.SendInterUnitCallOff(OSP5);
                                    form.TestGsmQuota(ref TestGSM_RVoithread, ref TestGSM_RDigThread);
                                    IsExportPDF = false;
                                }
                                else if (form.SelectedPageName == "功能试验")
                                {
                                    form.TrialFunction(ref TestLbjThread);
                                    IsExportPDF = false;
                                }
                                else if (form.SelectedPageName == "800M电性能测试")
                                {
                                    unit.ChangeVoiceToLBJ(OSP5);
                                    form.TestLbjQuota(ref TestLbjTranThread, ref TestLbjRecThread);
                                    IsExportPDF = false;
                                }
                            }
                            else
                            {
                                ExecuteSingleModTest(formName);
                                frm.SetBnImage(formName);
                                IsExportPDF = false;
                            }
                        }
                        catch (SystemException ex)
                        {

                        }
                    }
                    else
                    {
                        MessageBox.Show("请先输入测试员！");
                    }
                }
                else
                {
                    StopTest = true;
                    Suspend = true;
                    if (faTabStrip1.Items[0].Title == "整机测试")
                    {
                        StopTestMod();
                        TestTotalQuota = false;
                        //TestTotalSaved = true;
                        form.SetCheckBoxUnEdit(false);
                        form.DisposeObject();
                    }
                    else
                    {
                        StopTestMod();
                        frm.SetBnImage("停止测试");
                        SingleModform.SetCheckBoxUnEdit(false);
                        TransTestSaved = TrainEndTrialSaved = CoRecTestSaved = PiRecTestSaved =
                       SigTestSaved = GSM_RDigTestSaved = GSM_RFunTrialSaved = GSM_RVoTestSaved =
                       LBJFunTestSaved = true;
                    }
                    RunOrStop.Text = "开始测试";
                    RunOrStop.Image = CIRDiagnoseDevice.Properties.Resources.Play;
                    RunOrStop.ToolTipText = "开始测试";
                    PauseOrContinue.Text = "暂停测试";
                    PauseOrContinue.ToolTipText = "暂停测试";
                    PauseOrContinue.Image = CIRDiagnoseDevice.Properties.Resources.player_pause;
                    PauseOrContinue.Enabled = false;
                    SetToolStripStatusInfo("停止测试");
                }
            }
            catch (SystemException ex)
            {

            }
        }

        private void PauseOrContinue_Click(object sender, EventArgs e)
        {

            if (PauseOrContinue.Text == "暂停测试")
            {
                StripInfo = TestStatusLabel1.Text;
                SetToolStripStatusInfo("已暂停测试");
                PauseOrContinue.Text = "继续测试";
                PauseOrContinue.ToolTipText = "继续测试";
                PauseOrContinue.Image = CIRDiagnoseDevice.Properties.Resources.Play;
            }
            else
            {
                SetToolStripStatusInfo(StripInfo);
                PauseOrContinue.Image = CIRDiagnoseDevice.Properties.Resources.player_pause;
                PauseOrContinue.Text = "暂停测试";
                PauseOrContinue.ToolTipText = "暂停测试";

            }
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        public void AddTabPage(string title, Form frm)
        {
            FATabStripItem item = new FATabStripItem(title, null);
            int num = faTabStrip1.Items.Count;
            bool haveSame = false;
            int index = 0;
            for (int i = 0; i < num; i++)
            {
                float height = faTabStrip1.Items[i].StripRect.Height;
                int startX = (int)faTabStrip1.Items[i].StripRect.X + 1;
                FATabStripItem item1 = faTabStrip1.GetTabItemByPoint(new Point(startX, (int)height));
                if (item1 != null)
                {
                    if (item.Title == item1.Title)
                    {
                        haveSame = true;
                        index = i;
                    }
                }
            }
            if (!haveSame)
            {
                faTabStrip1.AddTab(item);
                frm.TopLevel = false;
                frm.Parent = this.faTabStrip1.Items[faTabStrip1.Items.Count - 1];
                frm.Dock = DockStyle.Fill;
                frm.FormBorderStyle = FormBorderStyle.None;
                frm.Show();
                faTabStrip1.SelectedItem = item;
            }
            else
            {
                faTabStrip1.SelectedItem = faTabStrip1.Items[index];
            }
        }

        public void InitTabPage()
        {
            int num = faTabStrip1.Items.Count;
            for (int i = 1; i < num; i++)
            {
                SingleModform = (SingleModFrm)faTabStrip1.Items[i].Controls["SingleModFrm"];
                string formName = faTabStrip1.Items[i].Title.Trim();
                SingleModform.ClearDataGridView(formName, SingleModform.dataGridView1);
            }
        }


        private void SaveTestRecord(string modName)
        {
            string ToolStripInfo = string.Empty;
            if (!IsSaveAll)
            {
                // if (preDev_num != curDev_num)
                {
                    if (!IsTestTotal)
                    {
                        if (modName == "LBJ功能试验")
                        {
                            if (!LBJFunTestSaved)
                            {
                                SaveLbjFunTrialData(false);
                            }
                            else
                            {
                                ToolStripInfo = "已保存的项";
                                SetToolStrip(ToolStripInfo);
                            }
                        }
                        else if (modName == "发射机性能指标测试")
                        {
                            if (!TransTestSaved)
                            {
                                SaveTranTestData(false);
                            }
                            else
                            {
                                ToolStripInfo = "已保存的项";
                                SetToolStrip(ToolStripInfo);
                            }
                        }
                        else if (modName == "450MHz列尾功能试验")
                        {
                            if (!TrainEndTrialSaved)
                            {
                                SaveTrainEndTrialData(false);
                            }
                            else
                            {
                                ToolStripInfo = "已保存的项";
                                SetToolStrip(ToolStripInfo);
                            }
                        }
                        else if (modName == "同频接收机性能指标测试")
                        {
                            if (!CoRecTestSaved)
                            {
                                SaveCoRecTestData(false);
                            }
                            else
                            {
                                ToolStripInfo = "已保存的项";
                                SetToolStrip(ToolStripInfo);
                            }
                        }
                        else if (modName == "异频接收机性能指标测试")
                        {
                            if (!PiRecTestSaved)
                            {
                                SavePiRecTestData(false);
                            }
                            else
                            {
                                ToolStripInfo = "已保存的项";
                                SetToolStrip(ToolStripInfo);
                            }
                        }
                        else if (modName == "信令指标测试")
                        {
                            if (!SigTestSaved)
                            {
                                SaveSigTestData(false);
                            }
                            else
                            {
                                ToolStripInfo = "已保存的项";
                                SetToolStrip(ToolStripInfo);
                            }
                        }
                        else if (modName == "GSM-R数据模块电性能测试")
                        {
                            if (!GSM_RDigTestSaved)
                            {
                                SaveGsmDigTestData(false);
                            }
                            else
                            {
                                ToolStripInfo = "已保存的项";
                                SetToolStrip(ToolStripInfo);
                            }
                        }
                        else if (modName == "GSM-R数据模块功能试验")
                        {
                            if (!GSM_RFunTrialSaved)
                            {
                                SaveGsmFunTrialData(false);
                            }
                            else
                            {
                                ToolStripInfo = "已保存的项";
                                SetToolStrip(ToolStripInfo);
                            }
                        }
                        else if (modName == "GSM-R语音模块电性能测试")
                        {
                            if (!GSM_RVoTestSaved)
                            {
                                SaveGsmVoTestData(false);
                            }
                            else
                            {
                                ToolStripInfo = "已保存的项";
                                SetToolStrip(ToolStripInfo);
                            }
                        }
                        else if (modName == "GSM-R数据模块单板电性能测试")
                        {
                            if (!GSM_RDigTestSaved)
                            {
                                SaveGsmDigTestData(false);
                            }
                            else
                            {
                                ToolStripInfo = "已保存的项";
                                SetToolStrip(ToolStripInfo);
                            }
                        }
                        else if (modName == "GSM-R语音模块单板电性能测试")
                        {
                            if (!GSM_RVoTestSaved)
                            {
                                SaveGsmVoTestData(false);
                            }
                            else
                            {
                                ToolStripInfo = "已保存的项";
                                SetToolStrip(ToolStripInfo);
                            }
                        }
                        else if (modName == "列车接近预警功能试验")
                        {
                            if (!TrainApproachTrialSaved)
                            {
                                SaveTrainApproachData(false);
                            }
                            else
                            {
                                ToolStripInfo = "已保存的项";
                                SetToolStrip(ToolStripInfo);
                            }
                        }
                        else if (modName == "450MHz调度命令功能试验")
                        {
                            if (!_450MDispatchTrialSaved)
                            {
                                Save450MDispatchTrialData(false);
                            }
                            else
                            {
                                ToolStripInfo = "已保存的项";
                                SetToolStrip(ToolStripInfo);
                            }
                        }
                        else if (modName == "LBJ发射机电性能测试")
                        {
                            if (!LbjTranTestSaved)
                            {
                                SaveLbjTranTestData(false);
                            }
                            else
                            {
                                ToolStripInfo = "已保存的项";
                                SetToolStrip(ToolStripInfo);
                            }
                        }
                        else if (modName == "LBJ接收机电性能测试")
                        {
                            if (!LbjRecTestSaved)
                            {
                                SaveLbjRecTestData(false);
                            }
                            else
                            {
                                ToolStripInfo = "已保存的项";
                                SetToolStrip(ToolStripInfo);
                            }
                        }
                        if (TransTestSaved && TrainEndTrialSaved && CoRecTestSaved && PiRecTestSaved &&
                       SigTestSaved && GSM_RDigTestSaved && GSM_RFunTrialSaved && GSM_RVoTestSaved &&
                       LBJFunTestSaved && TrainApproachTrialSaved && _450MDispatchTrialSaved && LbjTranTestSaved && LbjRecTestSaved)
                        {
                            preDev_num = curDev_num;
                        }
                    }
                    else
                    {

                    }
                }
                //else
                //{
                //    string str = "已保存的项";
                //    SetToolStrip(str);
                //}
            }
            else
            {
                //   if (preDev_num != curDev_num)
                {
                    if (!IsTestTotal)
                    {
                        if (!LBJFunTestSaved && modName == "LBJ功能试验")
                        {
                            SaveLbjFunTrialData(true);
                            LBJFunTestSaved = true;
                        }
                        if (!TransTestSaved && modName == "发射机性能指标测试")
                        {
                            SaveTranTestData(true);
                            TransTestSaved = true;
                        }
                        if (!TrainEndTrialSaved && modName == "450MHz列尾功能试验")
                        {
                            SaveTrainEndTrialData(true);
                            TrainEndTrialSaved = true;
                        }
                        if (!CoRecTestSaved && modName == "同频接收机性能指标测试")
                        {
                            SaveCoRecTestData(true);
                            CoRecTestSaved = true;
                        }
                        if (!PiRecTestSaved && modName == "异频接收机性能指标测试")
                        {
                            SavePiRecTestData(true);
                            PiRecTestSaved = true;
                        }
                        if (!SigTestSaved && modName == "信令指标测试")
                        {
                            SaveSigTestData(true);
                            SigTestSaved = true;
                        }
                        if (!GSM_RDigTestSaved && (modName == "GSM-R数据模块电性能测试" || modName == "GSM-R数据模块单板电性能测试"))
                        {
                            SaveGsmDigTestData(true);
                            GSM_RDigTestSaved = true;
                        }
                        if (!GSM_RFunTrialSaved && modName == "GSM-R数据模块功能试验")
                        {
                            SaveGsmFunTrialData(true);
                            GSM_RFunTrialSaved = true;
                        }
                        if (!GSM_RVoTestSaved && (modName == "GSM-R语音模块电性能测试" || modName == "GSM-R语音模块单板电性能测试"))
                        {
                            SaveGsmVoTestData(true);
                            GSM_RVoTestSaved = true;
                        }
                        if (!TrainApproachTrialSaved && modName == "列车接近预警功能试验")
                        {
                            SaveTrainApproachData(true);
                            TrainApproachTrialSaved = true;
                        }
                        if (!_450MDispatchTrialSaved && modName == "450MHz调度命令功能试验")
                        {
                            Save450MDispatchTrialData(true);
                            _450MDispatchTrialSaved = true;
                        }
                        if (!LbjRecTestSaved && modName == "LBJ接收机电性能测试")
                        {
                            SaveLbjRecTestData(true);
                            LbjRecTestSaved = true;
                        }
                        if (!LbjTranTestSaved && modName == "LBJ发射机电性能测试")
                        {
                            SaveLbjTranTestData(true);
                            LbjTranTestSaved = true;
                        }
                        if (TransTestSaved && TrainEndTrialSaved && CoRecTestSaved && PiRecTestSaved &&
                 SigTestSaved && GSM_RDigTestSaved && GSM_RFunTrialSaved && GSM_RVoTestSaved &&
                 LBJFunTestSaved && LbjTranTestSaved && LbjRecTestSaved)
                        {
                            preDev_num = curDev_num;
                        }
                        else
                        {

                        }
                    }
                    else
                    {

                    }
                }
                //else
                //{
                //    string str = "已保存的项";
                //    SetToolStrip(str);
                //}
            }
        }

        //设置状态栏信息
        private void SetToolStripStatusInfo(string str)
        {
            TestStatusLabel1.Text = str;
        }

        /// <summary>
        /// 设置状态栏显示正在测试项目信息
        /// </summary>
        /// <param name="str"></param>
        public void SetToolStrip(string str)
        {
            try
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    SetToolStripStatusInfo(str);
                }));
            }
            catch
            {

            }
        }

        /// <summary>
        /// 设置状态栏显示系统时间
        /// </summary>
        /// <param name="str"></param>
        public void SetToolStripLabel(string str)
        {
            try
            {
                this.Invoke(new MethodInvoker(delegate
                    {
                        systemTimeStatusLabel2.Text = str;
                    }));
            }
            catch
            {

            }
        }

        /// <summary>
        /// 设置状态栏显示本机IP
        /// </summary>
        /// <param name="ip"></param>
        public void ShowIP(string ip)
        {
            try
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    IPLable.Text = "本机IP：" + ip;
                }));
            }
            catch
            {

            }
        }

        public DateTime GetStatusLabelTime()
        {
            DateTime time = Convert.ToDateTime(systemTimeStatusLabel2.Text.Trim());
            return time;
        }

        /// <summary>
        /// 单项测试完成后更改测试按妞状态
        /// </summary>
        public void ChangeButtonText()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    RunOrStop.Text = "开始测试";
                    RunOrStop.ToolTipText = "开始测试";
                    RunOrStop.Image = CIRDiagnoseDevice.Properties.Resources.Play;
                    if (form != null)
                    {
                        form.SetCheckBoxUnEdit(false);
                    }
                    if (SingleModform != null)
                    {
                        SingleModform.SetCheckBoxUnEdit(false);
                    }
                }));
            }
            else
            {
                RunOrStop.Text = "开始测试";
                RunOrStop.ToolTipText = "开始测试";
                RunOrStop.Image = CIRDiagnoseDevice.Properties.Resources.Play;
                if (form != null)
                {
                    form.SetCheckBoxUnEdit(false);
                }
                if (SingleModform != null)
                {
                    SingleModform.SetCheckBoxUnEdit(false);
                }
            }
        }

        //获取单项测试的窗体对象
        FATabStripItem stripItem;
        private void faTabStrip1_TabStripItemSelectionChanged(TabStripItemChangedEventArgs e)
        {
            if (!IsTestTotal)
            {
                if (StopTest)
                {
                    SingleModform = (SingleModFrm)e.Item.Controls["SingleModFrm"];
                    stripItem = e.Item;
                    if (SingleModform != null)
                    {
                        formName = e.Item.Title.Trim();
                        if (RunOrStop.Enabled == true)
                            Export.Enabled = true;
                    }
                    else
                    {
                        Export.Enabled = false;
                    }
                }
                else
                {
                    if (SingleModform != null)
                    {
                        faTabStrip1.SelectedItem = stripItem;
                        // MessageBox.Show("正在测试中请勿选择其他项！");
                    }
                }
            }
        }

        # region 保存各模块测试数据

        public void Save450MDispatchTrialData(bool saveAll)
        {
            try
            {
                if (manage.SaveData(SingleModform.dataGridView1, TestTime, Tester, "450MHz调度命令", Dev_num, ref exportDataStruct.tableList, false, saveAll))
                {
                    _450MDispatchTrialSaved = true;
                }
                else
                {
                    _450MDispatchTrialSaved = false;
                }
                if (!DeviceInfoSaved)
                {
                    bool saved = manage.RestoreData(Dev_num, DeviceType.CIR);
                    if (saved)
                        DeviceInfoSaved = true;
                    else
                        DeviceInfoSaved = false;
                }
            }
            catch
            {

            }
        }

        public void SaveTrainApproachData(bool saveAll)
        {
            try
            {
                if (manage.SaveData(SingleModform.dataGridView1, TestTime, Tester, "列车接近预警", Dev_num, ref exportDataStruct.tableList, false, saveAll))
                {
                    TrainApproachTrialSaved = true;
                }
                else
                {
                    TrainApproachTrialSaved = false;
                }
                if (!DeviceInfoSaved)
                {
                    bool saved = manage.RestoreData(Dev_num, DeviceType.CIR);
                    if (saved)
                        DeviceInfoSaved = true;
                    else
                        DeviceInfoSaved = false;
                }
            }
            catch
            {

            }
        }

        public void SaveTranTestData(bool saveAll)
        {
            try
            {
                if (manage.SaveData(SingleModform.dataGridView1, TestTime, Tester, "450MHz发射机", Dev_num, ref exportDataStruct.tableList, true, saveAll))
                {
                    TransTestSaved = true;

                }
                else
                {
                    TransTestSaved = false;
                }
                if (!DeviceInfoSaved)
                {
                    bool saved = manage.RestoreData(Dev_num, DeviceType.CIR);
                    if (saved)
                        DeviceInfoSaved = true;
                    else
                        DeviceInfoSaved = false;
                }
            }
            catch
            {

            }
        }

        public void SaveCoRecTestData(bool saveAll)
        {
            try
            {
                if (manage.SaveData(SingleModform.dataGridView1, TestTime, Tester, "450MHz同频接收机", Dev_num, ref exportDataStruct.tableList, true, saveAll))
                {
                    CoRecTestSaved = true;

                }
                else
                {
                    CoRecTestSaved = false;
                }
                if (!DeviceInfoSaved)
                {
                    bool saved = manage.RestoreData(Dev_num, DeviceType.CIR);
                    if (saved)
                        DeviceInfoSaved = true;
                    else
                        DeviceInfoSaved = false;
                }
            }
            catch
            {

            }
        }

        public void SaveLbjTranTestData(bool saveAll)
        {
            try
            {
                if (manage.SaveData(SingleModform.dataGridView1, TestTime, Tester, "LBJ发射机", Dev_num, ref exportDataStruct.tableList, true, saveAll))
                {
                    LbjTranTestSaved = true;

                }
                else
                {
                    LbjTranTestSaved = false;
                }
                if (!DeviceInfoSaved)
                {
                    bool saved = manage.RestoreData(Dev_num, DeviceType.LBJ);
                    if (saved)
                        DeviceInfoSaved = true;
                    else
                        DeviceInfoSaved = false;
                }
            }
            catch
            {

            }
        }

        public void SaveLbjRecTestData(bool saveAll)
        {
            try
            {
                if (manage.SaveData(SingleModform.dataGridView1, TestTime, Tester, "LBJ接收机", Dev_num, ref exportDataStruct.tableList, true, saveAll))
                {
                    LbjRecTestSaved = true;

                }
                else
                {
                    LbjRecTestSaved = false;
                }
                if (!DeviceInfoSaved)
                {
                    bool saved = manage.RestoreData(Dev_num, DeviceType.LBJ);
                    if (saved)
                        DeviceInfoSaved = true;
                    else
                        DeviceInfoSaved = false;
                }
            }
            catch
            {

            }
        }

        public void SavePiRecTestData(bool saveAll)
        {
            try
            {
                if (manage.SaveData(SingleModform.dataGridView1, TestTime, Tester, "450MHz异频接收机", Dev_num, ref exportDataStruct.tableList, true, saveAll))
                {
                    PiRecTestSaved = true;

                }
                else
                {
                    PiRecTestSaved = false;
                }
                if (!DeviceInfoSaved)
                {
                    bool saved = manage.RestoreData(Dev_num, DeviceType.CIR);
                    if (saved)
                        DeviceInfoSaved = true;
                    else
                        DeviceInfoSaved = false;
                }
            }
            catch
            {

            }
        }

        public void SaveSigTestData(bool saveAll)
        {
            try
            {
                if (manage.SaveData(SingleModform.dataGridView1, TestTime, Tester, "450MHz信令", Dev_num, ref exportDataStruct.tableList, true, saveAll))
                {
                    SigTestSaved = true;

                }
                else
                {
                    SigTestSaved = false;
                }
                if (!DeviceInfoSaved)
                {
                    bool saved = manage.RestoreData(Dev_num, DeviceType.CIR);
                    if (saved)
                        DeviceInfoSaved = true;
                    else
                        DeviceInfoSaved = false;
                }
            }
            catch
            {

            }
        }

        public void SaveTrainEndTrialData(bool saveAll)
        {
            try
            {

                if (manage.SaveData(SingleModform.dataGridView1, TestTime, Tester, "450M列尾功能试验", Dev_num, ref exportDataStruct.tableList, false, saveAll))
                {
                    TrainEndTrialSaved = true;

                }
                else
                {
                    TrainEndTrialSaved = false;
                }
                if (!DeviceInfoSaved)
                {
                    bool saved = manage.RestoreData(Dev_num, DeviceType.CIR);
                    if (saved)
                        DeviceInfoSaved = true;
                    else
                        DeviceInfoSaved = false;
                }
            }
            catch
            {

            }
        }

        public void SaveGsmVoTestData(bool saveAll)
        {
            try
            {

                if (manage.SaveData(SingleModform.dataGridView1, TestTime, Tester, "GSM-R语音", Dev_num, ref exportDataStruct.tableList, false, saveAll))
                {
                    GSM_RVoTestSaved = true;

                }
                else
                {
                    GSM_RVoTestSaved = false;
                }
                if (!DeviceInfoSaved)
                {
                    bool saved = manage.RestoreData(Dev_num, DeviceType.CIR);
                    if (saved)
                        DeviceInfoSaved = true;
                    else
                        DeviceInfoSaved = false;
                }
            }
            catch
            {

            }
        }

        public void SaveGsmDigTestData(bool saveAll)
        {
            try
            {
                if (manage.SaveData(SingleModform.dataGridView1, TestTime, Tester, "GSM-R数据", Dev_num, ref exportDataStruct.tableList, false, saveAll))
                {
                    GSM_RDigTestSaved = true;

                }
                else
                {
                    GSM_RDigTestSaved = false;
                }
                if (!DeviceInfoSaved)
                {
                    bool saved = manage.RestoreData(Dev_num, DeviceType.CIR);
                    if (saved)
                        DeviceInfoSaved = true;
                    else
                        DeviceInfoSaved = false;
                }
            }
            catch
            {

            }
        }

        public void SaveGsmFunTrialData(bool saveAll)
        {
            try
            {
                if (manage.SaveData(SingleModform.dataGridView1, TestTime, Tester, "GSM-R功能试验", Dev_num, ref exportDataStruct.tableList, false, saveAll))
                {
                    GSM_RFunTrialSaved = true;

                }
                else
                {
                    GSM_RFunTrialSaved = false;
                }
                if (!DeviceInfoSaved)
                {
                    bool saved = manage.RestoreData(Dev_num, DeviceType.CIR);
                    if (saved)
                        DeviceInfoSaved = true;
                    else
                        DeviceInfoSaved = false;
                }
            }
            catch
            {
            }
        }

        public void SaveLbjFunTrialData(bool saveAll)
        {
            try
            {
                if (manage.SaveData(SingleModform.dataGridView1, TestTime, Tester, "LBJ功能试验", Dev_num, ref exportDataStruct.tableList, false, saveAll))
                {
                    LBJFunTestSaved = true;

                }
                else
                {
                    LBJFunTestSaved = false;
                }
                if (!DeviceInfoSaved)
                {
                    bool saved = manage.RestoreData(Dev_num, DeviceType.LBJ);
                    if (saved)
                        DeviceInfoSaved = true;
                    else
                        DeviceInfoSaved = false;
                }
            }
            catch
            {

            }
        }

        #  endregion

        #  region 停止正在测试的模块

        /// <summary>
        /// 停止正在测试的项
        /// </summary>
        public void StopTestMod()
        {
            MainFrm.MotherFrm.StopTest = true;
            try
            {
                if (TestHeLuQiThread != null)
                {
                    if (TestHeLuQiThread.IsAlive)
                    {
                        TestHeLuQiThread.Abort();
                        TestHeLuQiThread = null;
                    }
                }
                if (Test450M_CoRecThread != null)
                {
                    if (Test450M_CoRecThread.IsAlive)
                    {
                        Test450M_CoRecThread.Abort();
                        Test450M_CoRecThread = null;
                    }
                }
                if (GSMFunTrialThread != null)
                {
                    if (GSMFunTrialThread.IsAlive)
                    {
                        GSMFunTrialThread.Abort();
                        GSMFunTrialThread = null;
                    }
                }
                if (Test450M_PiRecThread != null)
                {
                    if (Test450M_PiRecThread.IsAlive)
                    {
                        Test450M_PiRecThread.Abort();
                        Test450M_PiRecThread = null;
                    }
                }
                if (Test450M_SigThread != null)
                {
                    if (Test450M_SigThread.IsAlive)
                    {
                        Test450M_SigThread.Abort();
                        Test450M_SigThread = null;
                    }
                }
                if (Test450M_TranThread != null)
                {
                    if (Test450M_TranThread.IsAlive)
                    {
                        Test450M_TranThread.Abort();
                        Test450M_TranThread = null;
                    }
                    CIR_450M_TranModTest.TimerEnable = false;
                }
                if (TestGSM_RDigThread != null)
                {
                    if (TestGSM_RDigThread.IsAlive)
                    {
                        TestGSM_RDigThread.Abort();
                        TestGSM_RDigThread = null;
                    }
                    if (SingleModform != null)
                        SingleModform.CloseSerialPort();
                }
                if (TestGSM_RVoithread != null)
                {
                    if (TestGSM_RVoithread.IsAlive)
                    {
                        TestGSM_RVoithread.Abort();
                        TestGSM_RVoithread = null;
                    }
                    TestGSM_RVo = false;
                    if (SingleModform != null)
                        SingleModform.ClearSource();
                }
                if (TestLbjThread != null)
                {
                    if (TestLbjThread.IsAlive)
                    {
                        TestLbjThread.Abort();
                        LBJFunTrial.TimerEnable = false;
                        TestLbjThread = null;
                    }
                    if (LbjSerialPort.IsOpen)
                    {
                        try
                        {
                            LbjSerialPort.Close();
                        }
                        catch { }
                    }
                }
                if (DealATCommThread != null)
                {
                    if (DealATCommThread.IsAlive)
                    {
                        DealATCommThread.Abort();
                        DealATCommThread = null;
                    }
                    if (GSM_RModPort.IsOpen)
                    {
                        StopTest = true;
                        GSM_RModPort.Close();
                    }
                }
                if (Test450MDispatchThread != null)
                {
                    if (Test450MDispatchThread.IsAlive)
                    {
                        Test450MDispatchThread.Abort();
                        Test450MDispatchThread = null;
                    }
                    if (TrainEndSerialPort.IsOpen)
                    {
                        try
                        {
                            TrainEndSerialPort.Close();
                        }
                        catch
                        {
                        }
                    }
                }
                if (Test450MTrainEndThread != null)
                {
                    if (Test450MTrainEndThread.IsAlive)
                    {
                        Test450MTrainEndThread.Abort();
                        Test450MTrainEndThread = null;
                    }
                    _450MFunTrial.TimerEnable = false;
                    if (TrainEndSerialPort.IsOpen)
                    {
                        try
                        {
                            TrainEndSerialPort.Close();
                        }
                        catch
                        {
                        }
                    }
                }
                if (TrainApproachTrialThread != null)
                {
                    if (TrainApproachTrialThread.IsAlive)
                    {
                        TrainApproachTrialThread.Abort();
                        TrainApproachTrialThread = null;
                    }
                    if (PocsagCodePort.IsOpen)
                    {
                        try
                        {
                            PocsagCodePort.Close();
                        }
                        catch
                        {

                        }
                    }
                }
                if (TestLbjTranThread != null)
                {
                    if (LbjNorSerialPort.IsOpen)
                    {
                        ExitLbjNorTest(LbjNorSerialPort);
                    }
                    if (TestLbjTranThread.IsAlive)
                    {
                        TestLbjTranThread.Abort();
                        TestLbjTranThread = null;
                    }
                }
                if (TestLbjRecThread != null)
                {
                    if (LbjNorSerialPort.IsOpen)
                    {
                        ExitLbjNorTest(LbjNorSerialPort);
                    }
                    if (TestLbjRecThread.IsAlive)
                    {
                        TestLbjRecThread.Abort();
                        TestLbjRecThread = null;
                    }
                }
                if (_2944SerialPort.IsOpen)
                {
                    try
                    {
                        StopTest = true;
                        _2944SerialPort.Close();
                    }
                    catch
                    {

                    }
                }
                if (_4202SerialPort.IsOpen)
                {
                    try
                    {
                        _4202SerialPort.Close();
                    }
                    catch
                    {

                    }
                }
                if (LbjNorSerialPort.IsOpen)
                {
                    try
                    {
                        StopTest = true;
                        LbjNorSerialPort.Close();
                    }
                    catch
                    {

                    }
                }

                if (OSP5.IsOpen)
                {
                    try
                    {
                        OSP5.Close();
                    }
                    catch
                    {
                    }
                }
                if (SingleModform != null)
                {
                    SingleModform.ClearSource();
                }
            }
            catch (Exception ex1)
            {

            }
        }

        private void ExitLbjNorTest(SerialPort OSP)
        {
            if (LbjManufName == "深圳长龙")
            {
                lbjCommand.QuitSingleTestCondition(OSP);
                unit.Delay(600);
                lbjCommand.QuitTestCondition(OSP);
            }
            if (LbjManufName == "上海通信")
            {
                lbjCommand.QuitSingleTest(OSP);
                unit.Delay(600);
                lbjCommand.QuitTest(OSP);
            }
        }
        #  endregion

        #   region  读取设备信息
        private void simpleButton1_Click_1(object sender, EventArgs e)
        {
            if (!IsTestTotal)
            {
                if (SingleModform != null)
                {
                    if (TransTestSaved && TrainEndTrialSaved && CoRecTestSaved && PiRecTestSaved &&
                        SigTestSaved && GSM_RDigTestSaved && GSM_RFunTrialSaved && GSM_RVoTestSaved &&
                        LBJFunTestSaved)
                    {
                        try
                        {
                            if (textEdit4.Text != "")
                            {
                                saveFileDialog1.FileName = device.Dev_num + " 设备测试记录";
                                if (curDev_num != textEdit4.Text.Trim())
                                {
                                    DbManage dbMana = new DbManage();
                                    DataTable dt = dbMana.QueryDb(textEdit4.Text.Trim());

                                    if (dt.Rows.Count != 0)
                                    {
                                        DataRow dr = dt.Rows[0];
                                        device.Cab_bur = dr[1].ToString();
                                        device.Cab_seg = dr[2].ToString();
                                        device.Cab_pl = dr[3].ToString();
                                        device.Cab_num = dr[4].ToString();
                                        device.Cab_db = Convert.ToInt32(dr[5].ToString().Trim());
                                        device.Cab_xc = dr[6].ToString();
                                        device.Dev_bur = dr[7].ToString();
                                        device.Dev_seg = dr[8].ToString();
                                        device.Dev_ws = dr[9].ToString();
                                        device.Dev_wa = dr[10].ToString();
                                        device.Dev_man = dr[11].ToString();
                                        device.Dev_mod = dr[12].ToString();
                                        device.Dev_num = dr[13].ToString();
                                        device.Dev_xc = dr[14].ToString();
                                        Dev_num = textEdit4.Text.Trim();
                                        curDev_num = device.Dev_num;
                                        InitTabPage();
                                        exportDataStruct.tableList.Clear();
                                        exportDataStruct.tableNameList.Clear();

                                        RunOrStop.Enabled = true;
                                        MessageBox.Show("设备信息读取成功,请确定软件上选择的设备生产商\r\n\r\n与待测CIR设备生产商一致,并选择测试频率后再进行测试！", "提示");
                                    }
                                    else
                                    {
                                        MessageBox.Show("该设备信息不存在，请添加设备信息！");
                                        DeviceInfoFrm deviceInfo = new DeviceInfoFrm();
                                        deviceInfo.Show();
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("设备信息已经读取!");
                                }
                            }
                            else
                            {
                                MessageBox.Show("请输入CIR设备编号！");
                            }
                        }
                        catch
                        {

                        }
                    }
                    else
                    {
                        textEdit4.Text = curDev_num;
                        MessageBox.Show("请先保存测试数据！");
                    }
                }
                else
                {
                    MessageBox.Show("请先选择测试项目后再读取设备信息！");
                }
            }
            else
            {
                if (TestTotalSaved)
                {
                    if (textEdit4.Text != "")
                    {
                        saveFileDialog1.FileName = device.Dev_num + " 设备测试记录";
                        if (curDev_num != textEdit4.Text.Trim())
                        {
                            DbManage dbMana = new DbManage();
                            DataTable dt = dbMana.QueryDb(textEdit4.Text.Trim());

                            if (dt.Rows.Count != 0)
                            {
                                DataRow dr = dt.Rows[0];
                                device.Cab_bur = dr[1].ToString();
                                device.Cab_seg = dr[2].ToString();
                                device.Cab_pl = dr[3].ToString();
                                device.Cab_num = dr[4].ToString();
                                device.Cab_db = Convert.ToInt32(dr[5].ToString().Trim());
                                device.Cab_xc = dr[6].ToString();
                                device.Dev_bur = dr[7].ToString();
                                device.Dev_seg = dr[8].ToString();
                                device.Dev_ws = dr[9].ToString();
                                device.Dev_wa = dr[10].ToString();
                                device.Dev_man = dr[11].ToString();
                                device.Dev_mod = dr[12].ToString();
                                device.Dev_num = dr[13].ToString();
                                device.Dev_xc = dr[14].ToString();
                                Dev_num = textEdit4.Text.Trim();
                                if (curDev_num != null)
                                {
                                    curDev_num = device.Dev_num;
                                    form.ClearDataGrid();
                                    exportDataStruct.tableNameList.Clear();
                                    exportDataStruct.tableList.Clear();
                                }
                                RunOrStop.Enabled = true;
                                MessageBox.Show("设备信息读取成功, \r\n\r\n请确定软件上选择的设备生产商\r\n\r\n与待测CIR设备生产商一致后再进行测试！");
                            }
                            else
                            {
                                MessageBox.Show("该设备信息不存在，请添加设备信息！");
                                DeviceInfoFrm deviceFrm = new DeviceInfoFrm();
                                deviceFrm.Show();
                            }
                        }
                        else
                        {
                            MessageBox.Show("设备信息已经读取!");
                        }
                    }
                    else
                    {
                        MessageBox.Show("请输入CIR设备编号！");
                    }
                }
                else
                {
                    textEdit4.Text = curDev_num;
                    MessageBox.Show("请先保存测试数据！");
                }
            }
        }

        #  endregion

        #    region 保存数据快捷键
        private void Save_Click(object sender, EventArgs e)
        {
            IsSaveAll = false;
            int rowNum = 0;
            if (!IsTestTotal)
            {
                if (textEdit3.Text != "")
                {
                    rowNum = CalculateDataRow(SingleModform.dataGridView1);
                    if (rowNum > 0)
                    {
                        SaveTestRecord(formName);
                    }
                    else
                    {
                        MessageBox.Show("没有数据要保存!");
                    }
                }
                else
                {
                    MessageBox.Show("请输入测试员姓名！");
                }
            }
            else
            {
                if (IsTestTotal == true && TestTotalSaved == false)
                {
                    if (textEdit3.Text != "")//测试员名字不为空
                    {

                        rowNum = 0;
                        if (form.SelectedPageName == "450M电性能指标测试")
                        {

                            rowNum = CalculateDataRow(form.dataGridView1);
                            if (rowNum > 0)
                            {
                                if (!_450MQuotaTestSaved)
                                {
                                    if (manage.SaveData(form.dataGridView1, TestTime, Tester, Dev_num, ref exportDataStruct.tableList, true))
                                    {
                                        _450MQuotaTestSaved = true;
                                        TransTestSaved = true;
                                        CoRecTestSaved = true;
                                        PiRecTestSaved = true;
                                        SigTestSaved = true;
                                        MessageBox.Show("数据保存成功！");
                                    }
                                    else
                                    {
                                        MessageBox.Show("数据保存失败！");
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("数据已经保存！");
                                }
                            }
                            else
                            {
                                MessageBox.Show("没有数据要保存!");
                            }
                        }
                        rowNum = 0;
                        if (form.SelectedPageName == "GSM-R电性能指标测试")
                        {
                            rowNum = CalculateDataRow(form.dataGridView2);
                            if (rowNum > 0)
                            {
                                if (!GsmQuotaTestSaved)
                                {
                                    if (manage.SaveData(form.dataGridView2, TestTime, Tester, Dev_num, ref exportDataStruct.tableList, false))
                                    {
                                        GsmQuotaTestSaved = true;
                                        GSM_RDigTestSaved = true;
                                        GSM_RVoTestSaved = true;
                                        MessageBox.Show("数据保存成功！");
                                    }
                                    else
                                    {
                                        MessageBox.Show("数据保存失败！");
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("数据已经保存！");
                                }
                            }
                            else
                            {
                                MessageBox.Show("没有数据要保存!");
                            }
                        }
                        rowNum = 0;
                        if (form.SelectedPageName == "功能试验")
                        {
                            rowNum = CalculateDataRow(form.dataGridView4);
                            if (rowNum > 0)
                            {
                                if (!WholeFunTrialSaved)
                                {
                                    if (manage.SaveData(form.dataGridView4, TestTime, Tester, Dev_num, ref exportDataStruct.tableList, false))
                                    {
                                        WholeFunTrialSaved = true;
                                        GSM_RFunTrialSaved = true;
                                        LBJFunTestSaved = true;
                                        _450MDispatchTrialSaved = true;
                                        TrainApproachTrialSaved = true;
                                        TrainEndTrialSaved = true;
                                        MessageBox.Show("数据保存成功！");
                                    }
                                    else
                                    {
                                        MessageBox.Show("数据保存失败！");
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("数据已经保存！");
                                }
                            }
                            else
                            {
                                MessageBox.Show("没有数据要保存!");
                            }
                        }
                        rowNum = 0;
                        if (form.SelectedPageName == "800M电性能测试")
                        {
                            rowNum = CalculateDataRow(form.dataGridView3);
                            if (rowNum > 0)
                            {
                                if (!LbjTranTestSaved || !LbjRecTestSaved)
                                {
                                    if (manage.SaveData(form.dataGridView3, TestTime, Tester, Dev_num, ref exportDataStruct.tableList, true))
                                    {
                                        LbjTranTestSaved = true;
                                        LbjRecTestSaved = true;
                                        MessageBox.Show("数据保存成功！");
                                    }
                                    else
                                    {
                                        MessageBox.Show("数据保存失败！");
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("数据已经保存！");
                                }
                            }
                            else
                            {
                                MessageBox.Show("没有数据要保存!");
                            }
                        }
                        if (MainFrm.MotherFrm.WholeFunTrialSaved && MainFrm.MotherFrm.GsmQuotaTestSaved && MainFrm.MotherFrm._450MQuotaTestSaved)
                        {
                            TestTotalSaved = true;
                            TestTotalQuota = false;
                        }
                        else
                        {
                            TestTotalSaved = false;
                        }
                        if (!DeviceInfoSaved)
                        {
                            bool saved = manage.RestoreData(Dev_num, DeviceType.CIR);
                            if (saved)
                                DeviceInfoSaved = true;
                            else
                                DeviceInfoSaved = false;
                        }
                    }
                    else
                    {
                        MessageBox.Show("请输入测试员姓名！");
                    }
                }
                else
                {
                    MessageBox.Show("数据已经保存！");
                }
            }
        }
        #   endregion


        //打开与接口单元通信串口
        public void OpenSerialPort(SerialPort OSP)
        {
            OSP.DataBits = 8;
            OSP.StopBits = StopBits.One;
            if (!OSP.IsOpen)
            {
                try
                {
                    OSP.Open();
                }
                catch
                {

                }
            }
        }

        // 响应单项测试完成时消息处理事件
        public void OnMessageNotice(object sender, ThreadState e)
        {
            switch (e)
            {
                case ThreadState.End:
                    frm.SetBnImage("测试完成");
                    break;
                default:
                    break;
            }
        }

        // 响应单项测试完成时消息处理事件
        public void OnMessageNotice(object sender, ThreadRunState e)
        {
            switch (e)
            {
                case ThreadRunState.End:
                    frm.SetBnImage("测试完成");
                    break;
                default:
                    break;
            }
        }

        private void AddOrDeleUserMenuItem_Click(object sender, EventArgs e)
        {
            UserManager userM = new UserManager();
            userM.Show();
        }

        private void DeviceInfoMenuItem_Click(object sender, EventArgs e)
        {
            DeviceInfoFrm deviceFrm = new DeviceInfoFrm();
            deviceFrm.Show();
        }



        private void RecordStatisticMenuItem_Click(object sender, EventArgs e)
        {
            Statistics statistic = new Statistics();
            statistic.Show();
        }

        private void ExportSingle()
        {
            saveFileDialog1.FileName = device.Dev_num + form.SelectedPageName + " 设备测试记录";
            int rowNum = 0;
            if (form.SelectedPageName == "450M电性能指标测试")
            {
                rowNum = CalculateDataRow(form.dataGridView1);
                if (rowNum > 0)
                {
                    if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                    {
                        string filePath = saveFileDialog1.FileName;
                        string caption = device.Dev_num + "测试记录";
                        bool result = manage.ExportDataGridViewToPDF(caption, form.dataGridView1, device, Tester, true, filePath);
                        if (!result)
                        {
                            MessageBox.Show("文件导出失败！");
                        }
                        else
                        {
                            MessageBox.Show("文件导出成功！");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("当前项目无测试结果！");
                }
            }
            else if (form.SelectedPageName == "GSM-R电性能指标测试")
            {
                rowNum = CalculateDataRow(form.dataGridView2);
                if (rowNum > 0)
                {
                    if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                    {
                        string filePath = saveFileDialog1.FileName;
                        string caption = device.Dev_num + "测试记录";
                        bool result = manage.ExportDataGridViewToPDF(caption, form.dataGridView2, device, Tester, false, filePath);
                        if (!result)
                        {
                            MessageBox.Show("文件导出失败！");
                        }
                        else
                        {
                            MessageBox.Show("文件导出成功！");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("当前项目无测试结果！");
                }
            }
            else if (form.SelectedPageName == "功能试验")
            {
                rowNum = CalculateDataRow(form.dataGridView4);
                if (rowNum > 0)
                {
                    if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                    {
                        string filePath = saveFileDialog1.FileName;
                        string caption = device.Dev_num + "测试记录";
                        bool result = manage.ExportDataGridViewToPDF(caption, form.dataGridView4, device, Tester, false, filePath);
                        if (!result)
                        {
                            MessageBox.Show("文件导出失败！");
                        }
                        else
                        {
                            MessageBox.Show("文件导出成功！");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("当前项目无测试结果！");
                }
            }
        }

        private void ExportTotal()
        {
            if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                string filePath = saveFileDialog1.FileName;
                string caption = device.Dev_num + "测试记录";
                bool result = manage.ExportDataToPDF(caption, exportDataStruct.tableList, filePath);

                if (result)
                {
                    IsExportPDF = true;
                    exportDataStruct.tableList.Clear();
                    exportDataStruct.tableNameList.Clear();
                    MessageBox.Show("文件导出成功！");
                }
                else
                {
                    IsExportPDF = false;
                    MessageBox.Show("文件导出失败！");
                }
            }
        }

        private void ExportSingleTotal()
        {
            if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                string filePath = saveFileDialog1.FileName;
                string caption = device.Dev_num + "测试记录";
                bool result = manage.ExportDataToPDF(caption, exportDataStruct.tableList, filePath);

                if (result)
                {
                    IsExportPDF = true;
                    exportDataStruct.tableList.Clear();
                    exportDataStruct.tableNameList.Clear();
                    MessageBox.Show("文件导出成功！");
                }
                else
                {
                    IsExportPDF = false;
                    MessageBox.Show("文件导出失败！");
                }
            }
        }

        private void ExportSingleSing()
        {
            if (SingleModform != null)
            {
                saveFileDialog1.FileName = device.Dev_num + formName + "记录";
                if (formName == "发射机性能指标测试")
                {
                    if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                    {
                        string filePath = saveFileDialog1.FileName;
                        string caption = device.Dev_num + "测试记录";
                        bool result = manage.ExportDataGridViewToPDF(caption, SingleModform.dataGridView1, device, Tester, true, filePath);
                        if (!result)
                        {
                            MessageBox.Show("文件导出失败！");
                        }
                        else
                        {
                            MessageBox.Show("文件导出成功！");
                        }
                    }
                }
                else if (formName == "同频接收机性能指标测试")
                {
                    if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                    {
                        string filePath = saveFileDialog1.FileName;
                        string caption = device.Dev_num + "测试记录";
                        bool result = manage.ExportDataGridViewToPDF(caption, SingleModform.dataGridView1, device, Tester, true, filePath);
                        if (!result)
                        {
                            MessageBox.Show("文件导出失败！");
                        }
                        else
                        {
                            MessageBox.Show("文件导出成功！");
                        }
                    }
                }
                else if (formName == "异频接收机性能指标测试")
                {
                    if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                    {
                        string filePath = saveFileDialog1.FileName;
                        string caption = device.Dev_num + "测试记录";
                        bool result = manage.ExportDataGridViewToPDF(caption, SingleModform.dataGridView1, device, Tester, true, filePath);
                        if (!result)
                        {
                            MessageBox.Show("文件导出失败！");
                        }
                        else
                        {
                            MessageBox.Show("文件导出成功！");
                        }
                    }
                }
                else if (formName == "信令指标测试")
                {
                    if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                    {
                        string filePath = saveFileDialog1.FileName;
                        string caption = device.Dev_num + "测试记录";
                        bool result = manage.ExportDataGridViewToPDF(caption, SingleModform.dataGridView1, device, Tester, true, filePath);
                        if (!result)
                        {
                            MessageBox.Show("文件导出失败！");
                        }
                        else
                        {
                            MessageBox.Show("文件导出成功！");
                        }
                    }
                }
                else if (formName == "LBJ发射机电性能测试")
                {
                    if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                    {
                        string filePath = saveFileDialog1.FileName;
                        string caption = device.Dev_num + "测试记录";
                        bool result = manage.ExportDataGridViewToPDF(caption, SingleModform.dataGridView1, device, Tester, true, filePath);
                        if (!result)
                        {
                            MessageBox.Show("文件导出失败！");
                        }
                        else
                        {
                            MessageBox.Show("文件导出成功！");
                        }
                    }
                }
                else if (formName == "LBJ接收机电性能测试")
                {
                    if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                    {
                        string filePath = saveFileDialog1.FileName;
                        string caption = device.Dev_num + "测试记录";
                        bool result = manage.ExportDataGridViewToPDF(caption, SingleModform.dataGridView1, device, Tester, true, filePath);
                        if (!result)
                        {
                            MessageBox.Show("文件导出失败！");
                        }
                        else
                        {
                            MessageBox.Show("文件导出成功！");
                        }
                    }
                }

                else
                {
                    if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                    {
                        string filePath = saveFileDialog1.FileName;
                        string caption = device.Dev_num + "测试记录";
                        bool result = manage.ExportDataGridViewToPDF(caption, SingleModform.dataGridView1, device, Tester, false, filePath);
                        if (!result)
                        {
                            MessageBox.Show("文件导出失败！");
                        }
                        else
                        {
                            MessageBox.Show("文件导出成功！");
                        }
                    }
                }
            }
        }

        private void Export_Click(object sender, EventArgs e)
        {
            try
            {
                if (textEdit3.Text != "")
                {
                    if (StopTest)
                    {
                        if (IsTestTotal)
                        {
                            int rowNum = 0;
                            rowNum = CalculateDataRow(form.dataGridView1);
                            if (rowNum > 0)
                            {
                                if (exportDataStruct.tableNameList.Contains("450M电性能指标测试") == false)
                                {
                                    DataTable table = manage.CreateTotalTestExportTable(form.dataGridView1, true);
                                    exportDataStruct.tableList.Add(table);
                                    exportDataStruct.tableNameList.Add("450M电性能指标测试");
                                }
                            }
                            rowNum = 0;
                            rowNum = CalculateDataRow(form.dataGridView2);
                            if (rowNum > 0)
                            {
                                if (exportDataStruct.tableNameList.Contains("GSM-R电性能指标测试") == false)
                                {
                                    DataTable table = manage.CreateTotalTestExportTable(form.dataGridView2, false);
                                    exportDataStruct.tableNameList.Add("GSM-R电性能指标测试");
                                    exportDataStruct.tableList.Add(table);
                                }
                            }
                            rowNum = 0;
                            rowNum = CalculateDataRow(form.dataGridView4);
                            if (rowNum > 0)
                            {
                                if (exportDataStruct.tableNameList.Contains("功能试验") == false)
                                {
                                    DataTable table = manage.CreateTotalTestExportTable(form.dataGridView4, false);
                                    exportDataStruct.tableList.Add(table);
                                    exportDataStruct.tableNameList.Add("功能试验");
                                }
                            }
                            rowNum = 0;
                            rowNum = CalculateDataRow(form.dataGridView3);
                            if (rowNum > 0)
                            {
                                if (exportDataStruct.tableNameList.Contains("800M电性能指标测试") == false)
                                {
                                    DataTable table = manage.CreateTotalTestExportTable(form.dataGridView3, true);
                                    exportDataStruct.tableList.Add(table);
                                    exportDataStruct.tableNameList.Add("800M电性能指标测试");
                                }
                            }
                            if (exportDataStruct.tableList.Count > 0)
                            {
                                if (exportDataStruct.tableList.Count >= 2)
                                {
                                    if (MessageBox.Show("是否只导出当前测试结果？", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
                                    {
                                        saveFileDialog1.FileName = device.Dev_num + " 设备测试记录";
                                        ExportTotal();
                                    }
                                    else
                                    {
                                        ExportSingle();
                                    }
                                }
                                else
                                {
                                    ExportSingle();
                                }
                            }
                            else
                            {
                                MessageBox.Show("没有数据要导出！");
                            }
                        }
                        else
                        {
                            int rowNum = 0;
                            if (formName == "GSM-R语音模块单板电性能测试")
                            {
                                saveFileDialog1.FileName = device.Dev_num + formName + "记录";
                                if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                                {
                                    string filePath = saveFileDialog1.FileName;
                                    string caption = device.Dev_num + "测试记录";
                                    bool result = manage.ExportDataGridViewToPDF(caption, SingleModform.dataGridView1, device, Tester, false, filePath);
                                    if (!result)
                                    {
                                        MessageBox.Show("文件导出失败！");
                                    }
                                    else
                                    {
                                        MessageBox.Show("文件导出成功！");
                                    }
                                }
                                return;
                            }
                            else if (formName == "GSM-R数据模块单板电性能测试")
                            {
                                saveFileDialog1.FileName = device.Dev_num + formName + "记录";
                                if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                                {
                                    string filePath = saveFileDialog1.FileName;
                                    string caption = device.Dev_num + "测试记录";
                                    bool result = manage.ExportDataGridViewToPDF(caption, SingleModform.dataGridView1, device, Tester, false, filePath);
                                    if (!result)
                                    {
                                        MessageBox.Show("文件导出失败！");
                                    }
                                    else
                                    {
                                        MessageBox.Show("文件导出成功！");
                                    }
                                }
                                return;
                            }
                            else
                            {
                                for (int i = 1; i < faTabStrip1.Items.Count; i++)
                                {
                                    string frmName = faTabStrip1.Items[i].Title.Trim();
                                    SingleModFrm SigleModform = (SingleModFrm)faTabStrip1.Items[i].Controls["SingleModFrm"];
                                    if (exportDataStruct.tableNameList.Contains(formName) == false)
                                    {
                                        rowNum = CalculateDataRow(SigleModform.dataGridView1);
                                        if (rowNum > 0)
                                        {
                                            if (frmName == "发射机性能指标测试" || frmName == "同频接收机性能指标测试" || frmName == "异频接收机性能指标测试"
                                                || frmName == "信令指标测试" || frmName == "LBJ接收机电性能测试")
                                            {
                                                string modName = frmName;
                                                modName = modName.Trim(new char[] { '测', '试' });
                                                DataTable table = manage.CreateSingleTestExportTable(SigleModform.dataGridView1, modName, true);
                                                exportDataStruct.tableList.Add(table);
                                                exportDataStruct.tableNameList.Add(frmName);
                                            }
                                            else if (frmName == "LBJ发射机电性能测试")
                                            {
                                                DataTable table = manage.CreateTotalTestExportTable(SigleModform.dataGridView1, true);
                                                exportDataStruct.tableList.Add(table);
                                                exportDataStruct.tableNameList.Add(frmName);
                                            }
                                            else
                                            {
                                                string modName = frmName;
                                                modName = modName.Trim(new char[] { '测', '试' });
                                                DataTable table = manage.CreateSingleTestExportTable(SigleModform.dataGridView1, modName, false);
                                                exportDataStruct.tableList.Add(table);
                                                exportDataStruct.tableNameList.Add(frmName);
                                            }
                                        }
                                    }
                                }
                            }
                            if (exportDataStruct.tableList.Count > 0)
                            {
                                if (exportDataStruct.tableList.Count >= 2)
                                {
                                    if (MessageBox.Show("是否只导出当前测试结果？", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
                                    {
                                        saveFileDialog1.FileName = device.Dev_num + " 设备测试记录";
                                        ExportSingleTotal();
                                    }
                                    else
                                    {
                                        rowNum = CalculateDataRow(SingleModform.dataGridView1);
                                        if (rowNum > 0)
                                        {
                                            ExportSingleSing();
                                        }
                                        else
                                        {
                                            MessageBox.Show("当前测试项无测试结果！");
                                            return;
                                        }
                                    }
                                }
                                else
                                {
                                    ExportSingleSing();
                                }
                            }
                            else
                            {
                                MessageBox.Show("没有数据要导出！");
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("正在测试中请勿进行数据导出操作！");
                    }
                }
                else
                {
                    MessageBox.Show("请输入测试员姓名！");
                }
            }
            catch
            {
                MessageBox.Show("文件导出失败！");
            }
        }

        private void SaveAll_Click(object sender, EventArgs e)
        {
            IsSaveAll = true;
            if (textEdit3.Text != "")
            {
                if (IsTestTotal == true && TestTotalSaved == false)
                {
                    if (manage.SaveData(form.dataGridView1, TestTime, Tester, Dev_num, ref exportDataStruct.tableList, true))
                    {

                        _450MQuotaTestSaved = true;
                    }


                    if (manage.SaveData(form.dataGridView2, TestTime, Tester, Dev_num, ref exportDataStruct.tableList, false))
                    {

                        GsmQuotaTestSaved = true;
                    }

                    if (manage.SaveData(form.dataGridView4, TestTime, Tester, Dev_num, ref exportDataStruct.tableList, false))
                    {

                        WholeFunTrialSaved = true;
                    }
                    if (manage.SaveData(form.dataGridView3, TestTime, Tester, Dev_num, ref exportDataStruct.tableList, true))
                    {
                        LbjQuotaTestSaved = true;
                        manage.RestoreData(Dev_num, DeviceType.LBJ);
                    }
                    if (WholeFunTrialSaved && GsmQuotaTestSaved && _450MQuotaTestSaved && LbjQuotaTestSaved)
                    {
                        TestTotalSaved = true;
                        TestTotalQuota = false;
                        preDev_num = curDev_num;
                        TransTestSaved = true;
                        TrainEndTrialSaved = true;
                        CoRecTestSaved = true;
                        PiRecTestSaved = true;
                        SigTestSaved = true;
                        GSM_RDigTestSaved = true;
                        GSM_RFunTrialSaved = true;
                        GSM_RVoTestSaved = true;
                        LBJFunTestSaved = true;
                        _450MDispatchTrialSaved = true;
                        TrainApproachTrialSaved = true;
                        LbjTranTestSaved = true;
                        LbjRecTestSaved = true;
                    }
                    else
                    {
                        TestTotalSaved = false;
                    }

                    if (!DeviceInfoSaved)
                    {
                        bool saved = manage.RestoreData(Dev_num, DeviceType.CIR);
                        if (saved)
                            DeviceInfoSaved = true;
                        else
                            DeviceInfoSaved = false;
                    }
                }
                else
                {
                    if (TransTestSaved && TrainEndTrialSaved && CoRecTestSaved && PiRecTestSaved &&
                        SigTestSaved && GSM_RDigTestSaved && GSM_RFunTrialSaved && GSM_RVoTestSaved &&
                        LBJFunTestSaved && _450MDispatchTrialSaved && TrainApproachTrialSaved && LbjTranTestSaved && LbjRecTestSaved)
                    {
                        preDev_num = curDev_num;
                        MessageBox.Show("没有数据需要保存！");
                    }
                    else
                    {
                        for (int i = 1; i < faTabStrip1.Items.Count; i++)
                        {
                            string frmName = faTabStrip1.Items[i].Title.Trim();
                            SingleModform = (SingleModFrm)faTabStrip1.Items[i].Controls["SingleModFrm"];
                            SaveTestRecord(frmName);
                        }
                        if (TransTestSaved && TrainEndTrialSaved && CoRecTestSaved && PiRecTestSaved &&
                        SigTestSaved && GSM_RDigTestSaved && GSM_RFunTrialSaved && GSM_RVoTestSaved &&
                        LBJFunTestSaved && _450MDispatchTrialSaved && TrainApproachTrialSaved && LbjTranTestSaved && LbjRecTestSaved)
                        {
                            MessageBox.Show("数据保存成功！");
                        }
                        else
                        {
                            MessageBox.Show("数据保存失败！");
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("请输入测试员姓名！");
            }
        }

        private int CalculateDataRow(DataGridView dgv)
        {
            int num = 0;
            int columnNum = dgv.Columns.Count;
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                try
                {
                    if (columnNum > 3)
                    {
                        if (dgv.Rows[i].Cells["测试结果"].Value.ToString() != "")
                        {
                            num++;
                            break;
                        }
                    }
                    else
                    {
                        if (dgv.Rows[i].Cells["试验结果"].Value.ToString() != "")
                        {
                            num++;
                            break;
                        }
                    }
                }
                catch { }
            }
            return num;
        }

        private void printer_Click(object sender, EventArgs e)
        {
            try
            {
                if (textEdit3.Text != "")
                {
                    if (StopTest)
                    {
                        if (IsTestTotal && form != null)
                        {
                            if (form.SelectedPageName == "450M电性能指标测试")
                            {
                                int rowNum = CalculateDataRow(form.dataGridView1);
                                if (rowNum != 0)
                                {
                                    printRecord = new PrintRecord(null, Dev_num + form.SelectedPageName, TestTime, Tester, device, form.dataGridView1, true);
                                    printPreviewDialog1.ShowDialog();
                                }
                                else
                                {
                                    MessageBox.Show("没有数据要打印");
                                }
                            }
                            else if (form.SelectedPageName == "800M电性能指标测试")
                            {
                                int rowNum = CalculateDataRow(form.dataGridView3);
                                if (rowNum != 0)
                                {
                                    printRecord = new PrintRecord(null, Dev_num + form.SelectedPageName, TestTime, Tester, device, form.dataGridView2, true);
                                    printPreviewDialog1.ShowDialog();
                                }
                                else
                                {
                                    MessageBox.Show("没有数据要打印");
                                }
                            }
                            else if (form.SelectedPageName == "GSM-R电性能指标测试")
                            {
                                int rowNum = CalculateDataRow(form.dataGridView2);
                                if (rowNum != 0)
                                {
                                    printRecord = new PrintRecord(null, Dev_num + form.SelectedPageName, TestTime, Tester, device, form.dataGridView2, false);
                                    printPreviewDialog1.ShowDialog();
                                }
                                else
                                {
                                    MessageBox.Show("没有数据要打印");
                                }
                            }
                            else if (form.SelectedPageName == "功能试验")
                            {
                                int rowNum = CalculateDataRow(form.dataGridView4);
                                if (rowNum != 0)
                                {
                                    printRecord = new PrintRecord(null, Dev_num + form.SelectedPageName, TestTime, Tester, device, form.dataGridView4, false);
                                    printPreviewDialog1.ShowDialog();
                                }
                                else
                                {
                                    MessageBox.Show("没有数据要打印");
                                }
                            }
                            else if (form.SelectedPageName == "800M电性能测试")
                            {

                            }
                        }
                        else
                        {
                            if (SingleModform != null)
                            {
                                if (formName == "发射机性能指标测试")
                                {
                                    int rowNum = CalculateDataRow(SingleModform.dataGridView1);
                                    if (rowNum != 0)
                                    {
                                        printRecord = new PrintRecord(null, Dev_num + formName, TestTime, Tester, device, SingleModform.dataGridView1, true);
                                        printPreviewDialog1.ShowDialog();
                                    }
                                    else
                                    {
                                        MessageBox.Show("没有数据要打印");
                                    }
                                }
                                else if (formName == "GSM-R数据模块功能试验")
                                {
                                    int rowNum = CalculateDataRow(SingleModform.dataGridView1);
                                    if (rowNum != 0)
                                    {
                                        printRecord = new PrintRecord(null, Dev_num + formName, TestTime, Tester, device, SingleModform.dataGridView1, false);
                                        printPreviewDialog1.ShowDialog();
                                    }
                                    else
                                    {
                                        MessageBox.Show("没有数据要打印");
                                    }
                                }
                                else if (formName == "同频接收机性能指标测试")
                                {
                                    int rowNum = CalculateDataRow(SingleModform.dataGridView1);
                                    if (rowNum != 0)
                                    {
                                        printRecord = new PrintRecord(null, Dev_num + formName, TestTime, Tester, device, SingleModform.dataGridView1, true);
                                        printPreviewDialog1.ShowDialog();
                                    }
                                    else
                                    {
                                        MessageBox.Show("没有数据要打印");
                                    }
                                }
                                else if (formName == "异频接收机性能指标测试")
                                {
                                    int rowNum = CalculateDataRow(SingleModform.dataGridView1);
                                    if (rowNum != 0)
                                    {
                                        printRecord = new PrintRecord(null, Dev_num + formName, TestTime, Tester, device, SingleModform.dataGridView1, true);
                                        printPreviewDialog1.ShowDialog();
                                    }
                                    else
                                    {
                                        MessageBox.Show("没有数据要打印");
                                    }
                                }
                                else if (formName == "信令指标测试")
                                {
                                    int rowNum = CalculateDataRow(SingleModform.dataGridView1);
                                    if (rowNum != 0)
                                    {
                                        printRecord = new PrintRecord(null, Dev_num + formName, TestTime, Tester, device, SingleModform.dataGridView1, true);
                                        printPreviewDialog1.ShowDialog();
                                    }
                                    else
                                    {
                                        MessageBox.Show("没有数据要打印");
                                    }
                                }
                                else if (formName == "LBJ发射机电性能测试")
                                {
                                    int rowNum = CalculateDataRow(SingleModform.dataGridView1);
                                    if (rowNum != 0)
                                    {
                                        printRecord = new PrintRecord(null, Dev_num + formName, TestTime, Tester, device, SingleModform.dataGridView1, true);
                                        printPreviewDialog1.ShowDialog();
                                    }
                                    else
                                    {
                                        MessageBox.Show("没有数据要打印");
                                    }
                                }
                                else if (formName == "LBJ接收机电性能测试")
                                {
                                    int rowNum = CalculateDataRow(SingleModform.dataGridView1);
                                    if (rowNum != 0)
                                    {
                                        printRecord = new PrintRecord(null, Dev_num + formName, TestTime, Tester, device, SingleModform.dataGridView1, true);
                                        printPreviewDialog1.ShowDialog();
                                    }
                                    else
                                    {
                                        MessageBox.Show("没有数据要打印");
                                    }
                                }
                                else
                                {
                                    int rowNum = CalculateDataRow(SingleModform.dataGridView1);
                                    if (rowNum != 0)
                                    {
                                        printRecord = new PrintRecord(null, Dev_num + formName, TestTime, Tester, device, SingleModform.dataGridView1, false);
                                        printPreviewDialog1.ShowDialog();
                                    }
                                    else
                                    {
                                        MessageBox.Show("没有数据要打印");
                                    }
                                }
                            }
                            else
                            {
                                MessageBox.Show("没有数据要打印");
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("正在测试中请勿进行打印操作！");
                    }
                }
                else
                {
                    MessageBox.Show("请先输入测试员！");
                }
            }
            catch
            {

            }
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if (IsTestTotal)
            {
                if (form.SelectedPageName == "450M电性能指标测试" || form.SelectedPageName == "800M电性能指标测试")
                {
                    printRecord.PrintTestRecord(e);
                }
                else
                {
                    printRecord.PrintTotalTestResult(e);
                }
            }
            else
            {
                if (formName == "发射机性能指标测试")
                {
                    printRecord.PrintSingleTestData(e);
                }
                else if (formName == "同频接收机性能指标测试")
                {
                    printRecord.PrintSingleTestData(e);
                }
                else if (formName == "异频接收机性能指标测试")
                {
                    printRecord.PrintSingleTestData(e);
                }
                else if (formName == "信令指标测试")
                {
                    printRecord.PrintSingleTestData(e);
                }
                else if (formName == "GSM-R语音模块电性能测试" || formName == "GSM-R语音模块单板电性能测试")
                {
                    printRecord.PrintSingleTestData(e);
                }
                else if (formName == "GSM-R数据模块电性能测试" || formName == "GSM-R数据模块单板电性能测试")
                {
                    printRecord.PrintSingleTestData(e);
                }
                else if (formName == "LBJ功能试验")
                {
                    printRecord.PrintSingleTestData(e);
                }
                else if (formName == "450M列尾功能试验")
                {
                    printRecord.PrintSingleTestData(e);
                }
                else if (formName == "GSM-R数据模块功能试验")
                {
                    printRecord.PrintSingleTestData(e);
                }
                else if (formName == "LBJ发射机电性能测试")
                {
                    printRecord.PrintTestRecord(e);
                }
                else if (formName == "LBJ接收机电性能测试")
                {
                    printRecord.PrintSingleTestData(e);
                }
            }
        }

        private void textEdit3_EditValueChanged(object sender, EventArgs e)
        {
            Tester = textEdit3.Text.Trim();
        }

        private void MainFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!IsTestTotal)  //单项测试情况下退出
            {
                if (TransTestSaved && TrainEndTrialSaved && CoRecTestSaved && PiRecTestSaved &&
                        SigTestSaved && GSM_RDigTestSaved && GSM_RFunTrialSaved && GSM_RVoTestSaved &&
                        LBJFunTestSaved && TrainApproachTrialSaved && _450MDispatchTrialSaved && LbjRecTestSaved && LbjTranTestSaved)
                {
                    if (Login.logon != null)
                    {
                        Login.logon.Close();
                    }
                    this.Dispose();
                }
                else
                {
                    if (MessageBox.Show("是否保存测试数据！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        SaveAll.PerformClick();  //保存所有测试结果
                        e.Cancel = false;
                        if (Login.logon != null)
                        {
                            Login.logon.Close();
                        }
                        this.Dispose();
                    }
                    else
                    {
                        e.Cancel = false;
                        if (Login.logon != null)
                        {
                            Login.logon.Close();
                        }
                        this.Dispose();
                    }
                }
            }
            else  // 整机测试情况下退出
            {
                if (TestTotalQuota == true && TestTotalSaved == false)
                {
                    MessageBox.Show("请先保存测试数据！");
                    e.Cancel = true;
                }
                else
                {
                    if (Login.logon != null)
                    {
                        Login.logon.Close();
                    }
                    this.Dispose();
                }
            }
        }

        private void Open110VPower_Click(object sender, EventArgs e)
        {
            unit.SendOpen110Power(PowerPort);
        }

        private void Close110VPower_Click(object sender, EventArgs e)
        {
            unit.SendClose110Power(PowerPort);
        }

        private void Open24VPower_Click(object sender, EventArgs e)
        {
            unit.SendOpen24Power(PowerPort);
        }

        private void Close24VPower_Click(object sender, EventArgs e)
        {
            unit.SendClose24Power(PowerPort);
        }

        private void 测Test110VPower_Click(object sender, EventArgs e)
        {
            unit.SendTest110Power(PowerPort);
            Test110Power = true;
        }

        private void Test24VPower_Click(object sender, EventArgs e)
        {
            unit.SendTest24Power(PowerPort);
            Test24vPower = true;
        }

        #   region 设置CheckBox不可选/可选
        public void SetCheckBoxUnEdit(bool unEdit, DataGridView dataGridView1)
        {
            if (dataGridView1.InvokeRequired)
            {
                dataGridView1.Invoke(new MethodInvoker(delegate
                {
                    dataGridView1.Columns[0].ReadOnly = unEdit;
                }));
            }
            else
            {
                dataGridView1.Columns[0].ReadOnly = unEdit;
            }
        }
        #   endregion

        #   region 发送AT命令
        public void SendATComm(SerialPort OSP)
        {
            string commStr = "ATA\r\n";
            byte[] sendByte = System.Text.ASCIIEncoding.Default.GetBytes(commStr);
            if (OSP.IsOpen)
            {
                OSP.Write(sendByte, 0, sendByte.Length);
            }
        }

        #   endregion

        private void faTabStrip1_TabStripItemClosing(TabStripItemClosingEventArgs e)
        {
            if (StopTest)
            {
                if (!IsTestTotal)
                {
                    if (!TransTestSaved || !CoRecTestSaved || !PiRecTestSaved || !SigTestSaved || !GSM_RVoTestSaved ||
                         !GSM_RFunTrialSaved || !GSM_RDigTestSaved || !TrainEndTrialSaved || !LBJFunTestSaved || !_450MDispatchTrialSaved || !TrainApproachTrialSaved || !LbjTranTestSaved || !LbjRecTestSaved)
                    {
                        e.Cancel = true;
                        if (textEdit3.Text != "")
                        {
                            if (formName == "发射机性能指标测试")
                            {
                                if (TransTestSaved == false)
                                {
                                    if (MessageBox.Show("是否保存" + formName + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        SaveTestRecord(formName);
                                        unit.Delay(1500);
                                        e.Cancel = false;
                                    }
                                    else
                                    {
                                        TransTestSaved = true;
                                        e.Cancel = false;
                                    }
                                }
                                else
                                {
                                    e.Cancel = false;
                                }
                            }
                            else if (formName == "同频接收机性能指标测试")
                            {
                                if (CoRecTestSaved == false)
                                {
                                    if (MessageBox.Show("是否保存" + formName + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        SaveTestRecord(formName);
                                        unit.Delay(1500);
                                        e.Cancel = false;
                                    }
                                    else
                                    {
                                        CoRecTestSaved = true;
                                        e.Cancel = false;
                                    }
                                }
                                else
                                {
                                    e.Cancel = false;
                                }
                            }
                            else if (formName == "异频接收机性能指标测试")
                            {
                                if (PiRecTestSaved == false)
                                {
                                    if (CoRecTestSaved == false)
                                    {
                                        if (MessageBox.Show("是否保存" + formName + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                        {
                                            SaveTestRecord(formName);
                                            unit.Delay(1500);
                                            e.Cancel = false;
                                        }
                                        else
                                        {
                                            PiRecTestSaved = true;
                                            e.Cancel = false;
                                        }
                                    }
                                }
                                else
                                {
                                    e.Cancel = false;
                                }
                            }
                            else if (formName == "信令指标测试")
                            {
                                if (SigTestSaved == false)
                                {
                                    if (MessageBox.Show("是否保存" + formName + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        SaveTestRecord(formName);
                                        unit.Delay(1500);
                                        e.Cancel = false;
                                    }
                                    else
                                    {
                                        SigTestSaved = true;
                                        e.Cancel = false;
                                    }
                                }
                                else
                                {
                                    e.Cancel = false;
                                }
                            }
                            else if (formName == "GSM-R语音模块电性能测试")
                            {
                                if (GSM_RVoTestSaved == false)
                                {
                                    if (MessageBox.Show("是否保存" + formName + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        SaveTestRecord(formName);
                                        unit.Delay(1500);
                                        e.Cancel = false;
                                    }
                                    else
                                    {
                                        GSM_RVoTestSaved = true;
                                        e.Cancel = false;
                                    }
                                }
                                else
                                {
                                    e.Cancel = false;
                                }
                            }
                            else if (formName == "GSM-R数据模块电性能测试")
                            {
                                if (GSM_RDigTestSaved == false)
                                {
                                    if (MessageBox.Show("是否保存" + formName + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        SaveTestRecord(formName);
                                        unit.Delay(1500);
                                        e.Cancel = false;
                                    }
                                    else
                                    {
                                        GSM_RDigTestSaved = true;
                                        e.Cancel = false;
                                    }
                                }
                                else
                                {
                                    e.Cancel = false;
                                }
                            }
                            else if (formName == "LBJ功能试验")
                            {
                                if (LBJFunTestSaved == false)
                                {
                                    if (MessageBox.Show("是否保存" + formName + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        SaveTestRecord(formName);
                                        unit.Delay(1500);
                                        e.Cancel = false;
                                    }
                                    else
                                    {
                                        LBJFunTestSaved = true;
                                        e.Cancel = false;
                                    }
                                }
                                else
                                {
                                    e.Cancel = false;
                                }
                            }
                            else if (formName == "450MHz列尾功能试验")
                            {
                                if (TrainEndTrialSaved == false)
                                {
                                    if (MessageBox.Show("是否保存" + formName + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        SaveTestRecord(formName);
                                        unit.Delay(1500);
                                        e.Cancel = false;
                                    }
                                    else
                                    {
                                        TrainEndTrialSaved = true;
                                        _450MFunTrial.TimerEnable = false;
                                        e.Cancel = false;
                                    }
                                }
                                else
                                {
                                    e.Cancel = false;
                                }
                            }
                            else if (formName == "GSM-R数据模块功能试验")
                            {
                                if (GSM_RFunTrialSaved == false)
                                {
                                    if (MessageBox.Show("是否保存" + formName + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        SaveTestRecord(formName);
                                        unit.Delay(1500);
                                        e.Cancel = false;
                                    }
                                    else
                                    {
                                        GSM_RFunTrialSaved = true;
                                        e.Cancel = false;
                                    }
                                }
                                else
                                {
                                    e.Cancel = false;
                                }
                            }
                            else if (formName == "450MHz调度命令功能试验")
                            {
                                if (_450MDispatchTrialSaved == false)
                                {
                                    if (MessageBox.Show("是否保存" + formName + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        SaveTestRecord(formName);
                                        unit.Delay(1500);
                                        e.Cancel = false;
                                    }
                                    else
                                    {
                                        _450MDispatchTrialSaved = true;
                                        e.Cancel = false;
                                    }
                                }
                                else
                                {
                                    e.Cancel = false;
                                }
                            }
                            else if (formName == "列车接近预警功能试验")
                            {
                                if (TrainApproachTrialSaved == false)
                                {
                                    if (MessageBox.Show("是否保存" + formName + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        SaveTestRecord(formName);
                                        unit.Delay(1500);
                                        e.Cancel = false;
                                    }
                                    else
                                    {
                                        TrainApproachTrialSaved = true;
                                        e.Cancel = false;
                                    }
                                }
                                else
                                {
                                    e.Cancel = false;
                                }
                            }
                            else if (formName == "GSM-R语音模块单板电性能测试")
                            {
                                if (GSM_RVoTestSaved == false)
                                {
                                    if (MessageBox.Show("是否保存" + formName + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        SaveTestRecord(formName);
                                        unit.Delay(1500);
                                        e.Cancel = false;
                                    }
                                    else
                                    {
                                        GSM_RVoTestSaved = true;
                                        e.Cancel = false;
                                    }
                                }
                                else
                                {
                                    e.Cancel = false;
                                }
                            }
                            else if (formName == "GSM-R数据模块单板电性能测试")
                            {
                                if (GSM_RDigTestSaved == false)
                                {
                                    if (MessageBox.Show("是否保存" + formName + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        SaveTestRecord(formName);
                                        unit.Delay(1500);
                                        e.Cancel = false;
                                    }
                                    else
                                    {
                                        GSM_RDigTestSaved = true;
                                        e.Cancel = false;
                                    }
                                }
                                else
                                {
                                    e.Cancel = false;
                                }
                            }
                            else if (formName == "LBJ发射机电性能测试")
                            {
                                if (LbjTranTestSaved == false)
                                {
                                    if (MessageBox.Show("是否保存" + formName + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        SaveTestRecord(formName);
                                        unit.Delay(1500);
                                        e.Cancel = false;
                                    }
                                    else
                                    {
                                        LbjTranTestSaved = true;
                                        e.Cancel = false;
                                    }
                                }
                                else
                                {
                                    e.Cancel = false;
                                }
                            }
                            else if (formName == "LBJ接收机电性能测试")
                            {
                                if (LbjRecTestSaved == false)
                                {
                                    if (MessageBox.Show("是否保存" + formName + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        SaveTestRecord(formName);
                                        unit.Delay(1500);
                                        e.Cancel = false;
                                    }
                                    else
                                    {
                                        LbjRecTestSaved = true;
                                        e.Cancel = false;
                                    }
                                }
                                else
                                {
                                    e.Cancel = false;
                                }
                            }
                            if (faTabStrip1.Items.Count == 1)
                            {
                                frm.Close();
                                frm.Dispose();
                                frm = null;
                                panelControl1.Visible = false;
                                textEdit4.Text = "";
                                textEdit3.Text = "";
                                curDev_num = "";
                                preDev_num = "";
                                SetStripButtonEnable(false);
                            }
                        }
                        else
                        {
                            MessageBox.Show("请输入测试员姓名后再保存！");
                        }
                    }
                    else
                    {
                        if (faTabStrip1.Items.Count == 0 || faTabStrip1.Items.Count == 1)
                        {
                            ChangeButtonText();
                            if (IsGsmModTest)
                                IsGsmModTest = false;
                            if (faTabStrip1.Items.Count == 1)
                            {
                                frm.Close();
                                frm.Dispose();
                                frm = null;
                                panelControl1.Visible = false;
                                textEdit4.Text = "";
                                textEdit3.Text = "";
                                curDev_num = "";
                                preDev_num = "";
                                SetStripButtonEnable(false);
                            }
                        }
                        else if (faTabStrip1.Items.Count == 2)
                        {
                            textEdit4.Text = "";
                            textEdit3.Text = "";
                            curDev_num = "";
                            preDev_num = "";
                            SetStripButtonEnable(false);
                        }
                    }
                }
                else
                {
                    if (!TestTotalSaved)
                    {
                        if (MessageBox.Show("是否保存整机测试全部" + "结果！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            e.Cancel = true;
                            if (textEdit3.Text != "")
                            {
                                // if (IsTestTotal == true && TestTotalSaved == false)
                                {

                                    bool result = manage.SaveData(form.dataGridView1, TestTime, Tester, Dev_num, ref exportDataStruct.tableList, true);
                                    if (result)
                                    {
                                        exportDataStruct.tableNameList.Add("450M电性能指标测试");
                                    }
                                    bool result2 = manage.SaveData(form.dataGridView2, TestTime, Tester, Dev_num, ref exportDataStruct.tableList, false);
                                    if (result2)
                                    {
                                        exportDataStruct.tableNameList.Add("GSM-R电性能指标测试");
                                    }
                                    bool result3 = manage.SaveData(form.dataGridView4, TestTime, Tester, Dev_num, ref exportDataStruct.tableList, false);
                                    if (result3)
                                    {
                                        exportDataStruct.tableNameList.Add("功能试验");
                                    }
                                    bool result4 = manage.SaveData(form.dataGridView3, TestTime, Tester, Dev_num, ref exportDataStruct.tableList, true);
                                    if (result4)
                                    {
                                        exportDataStruct.tableNameList.Add("800M电性能测试");
                                        manage.RestoreData(Dev_num, DeviceType.LBJ);
                                    }
                                    if (result3 && result2 && result && result4)
                                    {
                                        TestTotalSaved = true;
                                        TestTotalQuota = false;
                                        preDev_num = curDev_num;
                                        TransTestSaved = true;
                                        TrainEndTrialSaved = true;
                                        CoRecTestSaved = true;
                                        PiRecTestSaved = true;
                                        SigTestSaved = true;
                                        GSM_RDigTestSaved = true;
                                        GSM_RFunTrialSaved = true;
                                        GSM_RVoTestSaved = true;
                                        LBJFunTestSaved = true;
                                        _450MDispatchTrialSaved = true;
                                        TrainApproachTrialSaved = true;
                                        LbjTranTestSaved = true;
                                        LbjRecTestSaved = true;
                                    }
                                    else
                                    {
                                        TestTotalSaved = false;
                                    }
                                    if (!DeviceInfoSaved)
                                    {
                                        bool saved = manage.RestoreData(Dev_num, DeviceType.CIR);
                                        if (saved)
                                            DeviceInfoSaved = true;
                                        else
                                            DeviceInfoSaved = false;
                                    }
                                    unit.Delay(1500);
                                    e.Cancel = false;
                                    form.Close();
                                    form.Dispose();
                                    form = null;
                                    ChangeButtonText();
                                }
                                if (faTabStrip1.Items.Count == 1)
                                {
                                    panelControl1.Visible = false;
                                    textEdit4.Text = "";
                                    textEdit3.Text = "";
                                    curDev_num = "";
                                    preDev_num = "";
                                    SetStripButtonEnable(false);
                                }
                            }
                            else
                            {
                                MessageBox.Show("请输入测试员姓名后再保存！");
                            }
                        }
                        else
                        {
                            TestTotalSaved = true;
                            e.Cancel = false;
                            IsTestTotal = false;
                            TransTestSaved = true;
                            TrainEndTrialSaved = true;
                            CoRecTestSaved = true;
                            PiRecTestSaved = true;
                            SigTestSaved = true;
                            GSM_RDigTestSaved = true;
                            GSM_RFunTrialSaved = true;
                            GSM_RVoTestSaved = true;
                            LBJFunTestSaved = true;
                            _450MDispatchTrialSaved = true;
                            TrainApproachTrialSaved = true;
                            LbjTranTestSaved = true;
                            LbjRecTestSaved = true;
                        }
                    }
                    else
                    {
                        form.Close();
                        form.Dispose();
                        form = null;
                        ChangeButtonText();
                        InitParameter();
                        if (faTabStrip1.Items.Count == 1)
                        {
                            panelControl1.Visible = false;
                            textEdit4.Text = "";
                            textEdit3.Text = "";
                            curDev_num = "";
                            preDev_num = "";
                            SetStripButtonEnable(false);
                        }
                    }
                }
            }
            else
            {
                e.Cancel = true;
                MessageBox.Show("请先停止测试后再关闭该页面！");
            }
        }

        private void SerialPortConfigMenuItem_Click(object sender, EventArgs e)
        {
            string filePath = System.Windows.Forms.Application.StartupPath;//System.Environment.CurrentDirectory;
            string pathto = filePath + @"\" + "SerialPortInfo.ini";
            System.Diagnostics.Process.Start(pathto);

        }

        private void SetStripButtonEnable(bool enabled)
        {
            Save.Enabled = enabled;
            SaveAll.Enabled = enabled;
            RunOrStop.Enabled = enabled;
            PauseOrContinue.Enabled = enabled;
            Export.Enabled = enabled;
            printer.Enabled = enabled;
        }

        private void TestMenu_Click(object sender, EventArgs e)
        {

        }

        private void 天津712_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
            if (menuItem.Text == "天津七一二")
            {
                if (menuItem.Name == "天津712")
                {
                    manufactureName = menuItem.Text.Trim();
                    label1.Text = "设备生产商：" + manufactureName;
                    天津712.Checked = true;
                    深圳长龙.Checked = false;
                    北京华铁.Checked = false;
                    北京希电.Checked = false;
                    上海复旦.Checked = false;
                    上海新干通.Checked = false;
                    杭州创联.Checked = false;
                    世纪东方.Checked = false;
                    河南辉煌.Checked = false;
                }
                if (menuItem.Name == "天津712L")
                {
                    LbjManufName = menuItem.Text.Trim();
                    label2.Text = "LBJ生产商：" + LbjManufName;
                    天津712L.Checked = true;
                    深圳长龙L.Checked = false;
                    //北京华铁L.Checked = false;
                    //北京希电L.Checked = false;
                    //上海复旦L.Checked = false;
                    上海通信L.Checked = false;
                    //杭州创联L.Checked = false;
                    北京世纪东方L.Checked = false;
                    //河南辉煌L.Checked = false;
                }

            }
            else if (menuItem.Text == "北京希电")
            {
                manufactureName = menuItem.Text.Trim();
                label1.Text = "设备生产商：" + manufactureName;
                天津712.Checked = false;
                深圳长龙.Checked = false;
                北京华铁.Checked = false;
                北京希电.Checked = true;
                上海复旦.Checked = false;
                上海新干通.Checked = false;
                杭州创联.Checked = false;
                世纪东方.Checked = false;
                河南辉煌.Checked = false;
            }
            else if (menuItem.Text == "北京华铁")
            {
                manufactureName = menuItem.Text.Trim();
                label1.Text = "设备生产商：" + manufactureName;
                天津712.Checked = false;
                深圳长龙.Checked = false;
                北京华铁.Checked = true;
                北京希电.Checked = false;
                上海复旦.Checked = false;
                上海新干通.Checked = false;
                杭州创联.Checked = false;
                世纪东方.Checked = false;
                河南辉煌.Checked = false;
            }
            else if (menuItem.Text == "上海通信")
            {
                if (menuItem.Name == "上海新干通")
                {
                    manufactureName = menuItem.Text.Trim();
                    label1.Text = "设备生产商：" + manufactureName;
                    天津712.Checked = false;
                    深圳长龙.Checked = false;
                    北京华铁.Checked = false;
                    北京希电.Checked = false;
                    上海复旦.Checked = false;
                    上海新干通.Checked = true;
                    杭州创联.Checked = false;
                    世纪东方.Checked = false;
                    河南辉煌.Checked = false;
                }
                if (menuItem.Name == "上海通信L")
                {
                    LbjManufName = menuItem.Text.Trim();
                    label2.Text = "LBJ生产商：" + LbjManufName;
                    天津712L.Checked = false;
                    深圳长龙L.Checked = false;
                    //北京华铁L.Checked = false;
                    //北京希电L.Checked = false;
                    //上海复旦L.Checked = false;
                    上海通信L.Checked = true;
                    //杭州创联L.Checked = false;
                    北京世纪东方L.Checked = false;
                    //河南辉煌L.Checked = false;
                }

            }
            else if (menuItem.Text == "上海复旦")
            {
                manufactureName = menuItem.Text.Trim();
                label1.Text = "设备生产商：" + manufactureName;
                天津712.Checked = false;
                深圳长龙.Checked = false;
                北京华铁.Checked = false;
                北京希电.Checked = false;
                上海复旦.Checked = true;
                上海新干通.Checked = false;
                杭州创联.Checked = false;
                世纪东方.Checked = false;
                河南辉煌.Checked = false;
            }
            else if (menuItem.Text == "杭州创联")
            {
                manufactureName = menuItem.Text.Trim();
                label1.Text = "设备生产商：" + manufactureName;
                天津712.Checked = false;
                深圳长龙.Checked = false;
                北京华铁.Checked = false;
                北京希电.Checked = false;
                上海复旦.Checked = false;
                上海新干通.Checked = false;
                杭州创联.Checked = true;
                世纪东方.Checked = false;
                河南辉煌.Checked = false;
            }
            else if (menuItem.Text == "深圳长龙")
            {
                if (menuItem.Name == "深圳长龙")
                {
                    manufactureName = menuItem.Text.Trim();
                    label1.Text = "设备生产商：" + manufactureName;
                    天津712.Checked = false;
                    深圳长龙.Checked = true;
                    北京华铁.Checked = false;
                    北京希电.Checked = false;
                    上海复旦.Checked = false;
                    上海新干通.Checked = false;
                    杭州创联.Checked = false;
                    世纪东方.Checked = false;
                    河南辉煌.Checked = false;
                }
                if (menuItem.Name == "深圳长龙L")
                {
                    LbjManufName = menuItem.Text.Trim();
                    label2.Text = "LBJ生产商：" + LbjManufName;
                    天津712L.Checked = false;
                    深圳长龙L.Checked = true;
                    //北京华铁L.Checked = false;
                    //北京希电L.Checked = false;
                    //上海复旦L.Checked = false;
                    上海通信L.Checked = false;
                    //杭州创联L.Checked = false;
                    北京世纪东方L.Checked = false;
                    //河南辉煌L.Checked = false;
                }

            }
            else if (menuItem.Text == "北京世纪东方")
            {
                if (menuItem.Name == "世纪东方")
                {
                    manufactureName = menuItem.Text.Trim();
                    label1.Text = "设备生产商：" + manufactureName;
                    天津712.Checked = false;
                    深圳长龙.Checked = false;
                    北京华铁.Checked = false;
                    北京希电.Checked = false;
                    上海复旦.Checked = false;
                    上海新干通.Checked = false;
                    杭州创联.Checked = false;
                    世纪东方.Checked = true;
                    河南辉煌.Checked = false;
                }
                if (menuItem.Name == "北京世纪东方L")
                {
                    LbjManufName = menuItem.Text.Trim();
                    label2.Text = "LBJ生产商：" + LbjManufName;
                    天津712L.Checked = false;
                    深圳长龙L.Checked = false;
                    //北京华铁L.Checked = false;
                    //北京希电L.Checked = false;
                    //上海复旦L.Checked = false;
                    上海通信L.Checked = false;
                    //杭州创联L.Checked = false;
                    北京世纪东方L.Checked = true;
                    //河南辉煌L.Checked = false;
                }

            }
            else if (menuItem.Text == "河南辉煌")
            {
                manufactureName = menuItem.Text.Trim();
                label1.Text = "设备生产商：" + manufactureName;
                天津712.Checked = false;
                深圳长龙.Checked = false;
                北京华铁.Checked = false;
                北京希电.Checked = false;
                上海复旦.Checked = false;
                上海新干通.Checked = false;
                杭州创联.Checked = false;
                世纪东方.Checked = false;
                河南辉煌.Checked = true;
            }
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("软件版本号为：V1.0.0.1");
        }

        private void mHzToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
            if (menuItem.Text == "457.7MHz")
            {
                TestFreSetValue = 457.7f;
                TestPiFreSetValue = TestFreSetValue + 10.0f;
                F4575.Checked = false;
                F4578.Checked = false;
                F4579.Checked = false;
                F45800.Checked = false;
                F4577.Checked = true;
            }
            else if (menuItem.Text == "458.000MHz")
            {
                TestFreSetValue = 458.000f;
                TestPiFreSetValue = TestFreSetValue + 10.0f;
                F4575.Checked = false;
                F4578.Checked = false;
                F4579.Checked = false;
                F45800.Checked = true;
                F4577.Checked = false;
            }
            else if (menuItem.Text == "457.5MHz")
            {
                TestFreSetValue = 457.5f;
                TestPiFreSetValue = TestFreSetValue + 10.0f;
                F4575.Checked = true;
                F4578.Checked = false;
                F4579.Checked = false;
                F45800.Checked = false;
                F4577.Checked = false;
            }
            else if (menuItem.Text == "457.925MHz")
            {
                TestFreSetValue = 457.925f;
                TestPiFreSetValue = TestFreSetValue + 10.0f;
                F4575.Checked = false;
                F4578.Checked = false;
                F4579.Checked = true;
                F45800.Checked = false;
                F4577.Checked = false;
            }
            else if (menuItem.Text == "457.825.MHz")
            {
                TestFreSetValue = 457.825f;
                TestPiFreSetValue = TestFreSetValue + 10.0f;
                F4575.Checked = false;
                F4578.Checked = true;
                F4579.Checked = false;
                F45800.Checked = false;
                F4577.Checked = false;
            }
        }

        private void 汇入配置文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TestIndexConfig tic = new TestIndexConfig();
            tic.xiugaizhibiao();
        }

        private void TestRecordMenuItem_Click(object sender, EventArgs e)
        {

            DataQuery dataQ = new DataQuery();
            dataQ.Show();

        }

        private void 使用汇入指标文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TestIndexConfig TIC = new TestIndexConfig();
            TIC.shiyonghuiruzhibiao();
        }

        private void 使用默认指标文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TestIndexConfig TIC = new TestIndexConfig();
            TIC.shiyongmorenzhibiao();
        }

        private void 下载指标配置模板ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string filePath = System.Windows.Forms.Application.StartupPath;//System.Environment.CurrentDirectory;
            string pathfrom = filePath + @"\" + "testconfigusermodel.xls";
            //设置文件类型  
            saveFileDialog1.Filter = " xls files(*.xls)|*.txt|All files(*.*)|*.*";
            //设置文件名称：
            saveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMdd") + "-" + "CIR测试指标配置文件.xls"; 
            //设置默认文件类型显示顺序  
            saveFileDialog1.FilterIndex = 2; 
            //保存对话框是否记忆上次打开的目录  
            saveFileDialog1.RestoreDirectory = true;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {

                File.Copy(pathfrom, saveFileDialog1.FileName);
            }

        }

    }
}
